generating unsimilar random numbers

Started by
19 comments, last by boomji 18 years ago
hi, how would i generate random numbers from say 1-10 but not have any repeating numbers in them. i'm going in circles trying to figure out how i'm going to use the random function in an array and check for any repeats. object was to develop a scatter algorythm and not have and objects intersect. so i'd generate random numbers and filter out the repeats. i'd really appreciate any heads up on even psuedo code. thank's a lot. b
remember what you see to see what you rememberb
Advertisement
Pseudo-random sequences have repeats. It happens. That's why it's called random. If you really don't want repeats, just keep grabbing random numbers until you get one that's not a repeat. You don't need to come up with some uber pseudo random algo that doesn't repeat.
Quote:Original post by jonahrowley
some uber pseudo random algo that doesn't repeat

It's called "shuffling".
OK, with a range of 1 to 10, that would work. Not exactly practical for larger ranges though..


hey guys,
well first of thanks(learnt abt shuffling in your post).
but i was not really gunning for that.

i'm struggling with the coding aspect.

i was thinking on these lines...
1 .i generate a random sequence and store it into an array variable.
2.Then i need to iterate through them to see if there are repeats.
3.If there are i need to change those by running the random function again.

if you could hlp me out with the coe or piont me to someplace.
i've googled it and hit a few topics but i'd reckon some one must have run into similar situations here especially.

thanks again.
b
remember what you see to see what you rememberb
this way you are hading for a possible disaster.
what if it goes cycling ad infinitum(meaning you keep random-get already picked numbers.then your program stalls) the best possible worse scenario would be to have a long period of looping the random generator especially when the many numbers have been already picked(not infinity but very nightmarish)
OTOH you could choose a random number, see if it is already picked and iterate through the array in a normal fashion until you find one that hasn't already been picked up)
P.S.I must tell you what you mean is practically the same with shuffling, except you memorize an arbitrary length sequence(and do not perform real random the second time).What I have said is appliable to both
I suggest to run shuffling once and memorize some key points,if not the whole sequence

Something like this might have its uses. Play with the numbers and see what you find:
int limit = 10;for (int i = 0; i < limit; ++i){    cout << int(pow(7.0, i+1.0)) % (limit+1) << endl;}
1: Create an vector containing the numbers 1-10: myVector
2: Use a random number generator to create a 2 random numbers: i and j
3: std::swap(myVector,myVector[j % ( myVector.size()-1 )]);<br>4: repeat steps 2-3 for a large number of iterations (relative to the length of the vector)<br>5: Now you have a random vector with no repeats.
Quote:Original post by Nitage
1: Create an vector containing the numbers 1-10: myVector
2: Use a random number generator to create a 2 random numbers: i and j
3: std::swap(myVector,myVector[j % ( myVector.size()-1 )]);<br>4: repeat steps 2-3 for a large number of iterations (relative to the length of the vector)<br>5: Now you have a random vector with no repeats.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br>replace steps 3 and 4 with std::random_shuffle(myVector.begin(),myVector.end()) for both better randomness and better performance.
Quote:Original post by Trap
Quote:Original post by Nitage
1: Create an vector containing the numbers 1-10: myVector
2: Use a random number generator to create a 2 random numbers: i and j
3: std::swap(myVector,myVector[j % ( myVector.size()-1 )]);<br>4: repeat steps 2-3 for a large number of iterations (relative to the length of the vector)<br>5: Now you have a random vector with no repeats.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br>replace steps 3 and 4 with std::random_shuffle(myVector.begin(),myVector.end()) for both better randomness and better performance.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>And for less control if you want to tweak the algorithm… and less educational value seeing as he seems to want to know what's going &#111;n.<br><br>BTW, we've all assumed he's using C++ - might not be.

This topic is closed to new replies.

Advertisement