943,740 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 712
  • C RSS
Jan 28th, 2009
0

How do I check if a number has already been randomly generated?

Expand Post »
Hey

The title is a bit confusing so Ill explain.

I have a function that returns random values from 0-4 for the computer (which has numberofplayers). Lets say number of players is 2. One is me and the other is the PC. I (myself thru a scanf and I being player 1) set my value for 2. How can I create some kind of for/do/if/etc that keeps generating numbers until player 2 has a number differerent than player 1 (me)? Obviously the 0-4 range and the number of player changes depending on the options I set.


If someone needs more explaining or some kind of code example, please go ahead and post.
Similar Threads
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Jan 28th, 2009
-1

Re: How do I check if a number has already been randomly generated?

Kinda important so bumping up...
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Jan 28th, 2009
1

Re: How do I check if a number has already been randomly generated?

>The title is a bit confusing so Ill explain.
Unless you didn't say what you meant, I understand your problem perfectly just from the title. Have you tried storing the used random numbers and then searching that list when you need another? A naive solution is pretty obvious and trivial to implement, so I'm leaning toward thinking that you're too lazy to put forth any real effort.

>Kinda important so bumping up...
Bumping is a fantastic way to make sure your question doesn't get answered. It's presumptuous to believe that you're more important than everyone else, and many of us don't want to help people like that.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 28th, 2009
0

Re: How do I check if a number has already been randomly generated?

Click to Expand / Collapse  Quote originally posted by Narue ...
Have you tried storing the used random numbers and then searching that list when you need another?
Obviously, this is the solution the problem is how to implant it.
Do I make another array storing all the numbers?
And/or how?
lets say we have player[n]
player[1] says 1 (because he typed it in)
player[2] gets a generated number of 3 (because the generator returned him that number)
player[3] gets a generated number of 2 (fine again)
player[4] gets a generated number of 1; No. The generator must regenerate another number because this number has been said by one of the players before him. This time 3. Again no because player[2] said 3. Another generation this time 0. Next player...
Next player[5] gets generated 0. Again. 1. Again. 3. Again. 1. Again. 3. Again 2. Again. 0. Again. 2. Again. 3. Again. 4. FINALLY gets a good number

Thats bascially what I want to do. Im just not sure how to do it. Im not asking for the actual code just ideas on how to do it.
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Jan 28th, 2009
0

Re: How do I check if a number has already been randomly generated?

Have an array for each possible number, have each element be a flag as to whether the number at that index has been generated.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 29th, 2009
0

Re: How do I check if a number has already been randomly generated?

So if this were a pack of cards, player1 would choose a card, and the others would be dealt a card from the remaining deck.

Think of it in these terms,
- shuffle the deck
- find card (and remove it)
- deal card (from the top of the deck, and remove it)
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 29th, 2009
0

Re: How do I check if a number has already been randomly generated?

Click to Expand / Collapse  Quote originally posted by Salem ...
So if this were a pack of cards, player1 would choose a card, and the others would be dealt a card from the remaining deck.

Think of it in these terms,
- shuffle the deck
- find card (and remove it)
- deal card (from the top of the deck, and remove it)
Can't be a card because you know there are only 1-12 cards. This number can be infinite.

And yes I already thought of the array but I wanted to check if there was a easier/better way. I guess I'll just do it with another array comparing the array which stores the numbers.
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Jan 29th, 2009
1

Re: How do I check if a number has already been randomly generated?

>This number can be infinite.
Not when you're limited by the data type. If you use int, the limit is 32,767. When a programmer says "infinite", I always cringe because it means he isn't thinking about edge cases.

>And yes I already thought of the array but I wanted
>to check if there was a easier/better way.
There are easier/better ways, but if you're having trouble implementing the obvious solution, you'll have even more trouble with the better ones. All you need to do is generate a new number, then search the stored numbers for a match. If there's a match, generate a new number. Repeat until there's not a match:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int exists ( int key, int list[], int size )
  5. {
  6. int i;
  7.  
  8. for ( i = 0; i < size; i++ ) {
  9. if ( list[i] == key )
  10. return 1;
  11. }
  12.  
  13. return 0;
  14. }
  15.  
  16. int main ( void )
  17. {
  18. int total_players;
  19.  
  20. printf ( "Number of players: " );
  21. fflush ( stdout );
  22.  
  23. if ( scanf ( "%d", &total_players ) == 1 ) {
  24. int *players = malloc ( total_players * sizeof *players );
  25.  
  26. if ( players != NULL ) {
  27. int i;
  28.  
  29. /* Generate unique random numbers for each player */
  30. for ( i = 0; i < total_players; i++ ) {
  31. do
  32. players[i] = rand() % total_players;
  33. while ( exists ( players[i], players, i ) );
  34. }
  35.  
  36. /* Display the generated numbers */
  37. for ( i = 0; i < total_players; i++ )
  38. printf ( "Player[%d]: %d\n", i + 1, players[i] );
  39.  
  40. free ( players );
  41. }
  42. }
  43.  
  44. return 0;
  45. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 29th, 2009
0

Re: How do I check if a number has already been randomly generated?

> Can't be a card because you know there are only 1-12 cards
So that's an array of 12, filled with whatever you want, and then shuffled.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Apriori Algorithm in C code
Next Thread in C Forum Timeline: I need help with my highscore list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC