| | |
How do I check if a number has already been randomly generated?
![]() |
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
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.
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.
>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.
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.
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
•
•
•
•
Have you tried storing the used random numbers and then searching that list when you need another?
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.
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
•
•
•
•
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)
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.
>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:
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:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int exists ( int key, int list[], int size ) { int i; for ( i = 0; i < size; i++ ) { if ( list[i] == key ) return 1; } return 0; } int main ( void ) { int total_players; printf ( "Number of players: " ); fflush ( stdout ); if ( scanf ( "%d", &total_players ) == 1 ) { int *players = malloc ( total_players * sizeof *players ); if ( players != NULL ) { int i; /* Generate unique random numbers for each player */ for ( i = 0; i < total_players; i++ ) { do players[i] = rand() % total_players; while ( exists ( players[i], players, i ) ); } /* Display the generated numbers */ for ( i = 0; i < total_players; i++ ) printf ( "Player[%d]: %d\n", i + 1, players[i] ); free ( players ); } } return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Arrays help? (VB.NET)
- Can I recive some feedback with my c++ program? (C++)
- Need help whit code (Pascal and Delphi)
- Random number Sequence Generation (C++)
- Random Parralel Array (C++)
- how to come out the pic randomly in picture boxes? (C#)
- random matrix generation (C)
- help me on my case study!!! (C)
- C++ arrays and random numbers (C++)
Other Threads in the C Forum
- Previous Thread: Apriori Algorithm in C code
- Next Thread: I need help with my highscore list
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






