# Random numbers

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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 ;-)

##### Share on other sites
Quote:
 Original post by Beer HunterIf 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!

##### Share on other sites
Quote:
 Original post by xstreme2000What 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 ;-)
Is there not some function like std::random_shuffle?

##### Share on other sites

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

##### Share on other sites
Quote:
Original post by Evil Steve
Quote:
 Original post by xstreme2000What 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 ;-)
Is there not some function like std::random_shuffle?

Yes, I do believe there is ;-)

##### Share on other sites
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.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 15
• 14
• ### Forum Statistics

• Total Topics
633391
• Total Posts
3011633
• ### Who's Online (See full list)

There are no registered users currently online

×