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

Reply

Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

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

 
0
  #1
Jan 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

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

 
-1
  #2
Jan 28th, 2009
Kinda important so bumping up...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #3
Jan 28th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

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

 
0
  #4
Jan 28th, 2009
Originally Posted by Narue View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

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

 
0
  #5
Jan 28th, 2009
Have an array for each possible number, have each element be a flag as to whether the number at that index has been generated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #6
Jan 29th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

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

 
0
  #7
Jan 29th, 2009
Originally Posted by Salem View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #8
Jan 29th, 2009
>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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #9
Jan 29th, 2009
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC