Random numbers
I'd like to generate a series of numbers between 1 and 10. That's no problem.
However, I'd like to make sure I don't get the same number twice in a row. I considered doing a while loop like:
number1 = rand();
number2 = rand();
while (number2 == number1) {
number2 = rand();
}
The problem I foresee with this method is that, theoretically, it could run forever. And although it problably won't it may cause my program to hang up for a short time.
Can anyone think of a way to not get the same number twice or what I could do with that number if it arises a second time.
Thanks,
Thomo.
When generating random numbers from a to b there is always a 1/(b-a+1) chance of repeating any given number. The only solution to this is to test for equality and discard if the numbers are unequal. Potentially, this could run for quite a while, however it is highly unlikely. The probability of choosing a number, and then choosing that same number again is 1/10, three times is 1/100, four times 1/1000. Unless you are doing this quite a bit (generating hundreds of thousands of random numbers) you are unlikely to see any runs of more than 5 or 6 in a row. Making seven successive calls to rand() is not terribly slow and I wouldn't think it would cause your program to hang.
If you absolutely have to avoid it, you can generate a random number from 1 to 9 and add one if it's greater than or equal to the previous number.
Thanks.
I'll see how the first situation fairs up. I agree that it probably won't take too many iterations to get a differen number. I after testing my program there is a problem I'll try the second.
I'll see how the first situation fairs up. I agree that it probably won't take too many iterations to get a differen number. I after testing my program there is a problem I'll try the second.
What you could do is rather than repeatedly try to generate the numbers 1 to 10 in a random order, you could create a list of numbers 1 to 10 and shuffle them randomly, that way you can guarentee that it wouldn't loop for ever.
Just an idea ;-)
Just an idea ;-)
Quote:Original post by Beer Hunter
If you absolutely have to avoid it, you can generate a random number from 1 to 9 and add one if it's greater than or equal to the previous number.
Of course, you get an uneven distribution of probability with that approach, because you're now twice as likely to get the number one more than the previous one, which may or may not be suitable to the application.
Thomo: I've used that particular technique you included in your inital post myself many times, and have never noticed any delays from its use. As jperlata wrote, it's so unlikely you'll get more than ten "retries" from your random number generator that it's not worth worrying about. I'm sure it will be fine!
Quote:Original post by xstreme2000Is there not some function like std::random_shuffle?
What you could do is rather than repeatedly try to generate the numbers 1 to 10 in a random order, you could create a list of numbers 1 to 10 and shuffle them randomly, that way you can guarentee that it wouldn't loop for ever.
Just an idea ;-)
Howabout ...
Create an array of size of 10. Then fill your array with numbers 1-10.
Then take two random numbers in range [0,9] and swap those array entries together. Repeat the steps several times.
- Doesn't require any checking and scales up pretty well.
Cheers
Create an array of size of 10. Then fill your array with numbers 1-10.
Then take two random numbers in range [0,9] and swap those array entries together. Repeat the steps several times.
- Doesn't require any checking and scales up pretty well.
Cheers
Quote:Original post by Evil SteveQuote:Original post by xstreme2000Is there not some function like std::random_shuffle?
What you could do is rather than repeatedly try to generate the numbers 1 to 10 in a random order, you could create a list of numbers 1 to 10 and shuffle them randomly, that way you can guarentee that it wouldn't loop for ever.
Just an idea ;-)
Yes, I do believe there is ;-)
Someone posted exactly the same question just a few days ago (well, 1-6 instead of 1-10). Here is the thread: Random Selection Problem
Take a look at janta's reply. It is the best solution and it hasn't been mentioned here yet.
Take a look at janta's reply. It is the best solution and it hasn't been mentioned here yet.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement