Jump to content
  • Advertisement
Sign in to follow this  
Thomo

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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!

Share this post


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

Share this post


Link to post
Share on other sites
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

Share this post


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


Yes, I do believe there is ;-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!