Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dark Star

Generating evenly distributed random numbers.

This topic is 5372 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

hi Guys, Ive made tetris clone, but I am not happy with the random number sequence I get when using rand() to get a new number which is the index of the next block to play. Is there a way of making the random numbers I get more evenly distributed so I dont get so many repeats of the same block so often. Thanks DarkStar UK ------------------------------- Loves cross-posting because it works

Share this post


Link to post
Share on other sites
Advertisement
you could store the last index in a variable and then, at create-piece time, compare... if they''re the same, then rand() again.

it may be an alternative

Share this post


Link to post
Share on other sites
Are you seeding the random number generator with something like:
srand(time(0));

?

Just because you get the same block twice doesn''t meant it isn''t random I hope you know.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I hope you''re not making something like

if (number < largest_piece_index) number = largest_piece_index; 


''Cause that would make piece # largest_piece_index show up more often than the rest.

If you have largest_piece_index types of pieces, this should over time generate a completely uniform distribution of pieces:

number = rand() % largest_piece_index 

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
Actually, if you read up on it, using the modulous operator (%) does not distribute the numbers evenly.


If rand() creates a list of random numbers from zero to n, then rand() % x will be equally random as long as n % x == 0. If n is much greater than x, it will be hard to tell that rand() % x is creating a slightly less random list when n % x != 0.

This whole discussion is a little off, mathematically. The idea of a list of random numbers that you force to be evenly distributed is a contradiction in itself. If you have a good random number generator, the list will probably eventually become pretty well distributed, but there are no guarantees.

The rand() function in stdlib works pretty well - if you are finding that your numbers are horribly distributed, even in long lists, then you are probably doing something wrong. Can you post the line you''re using to pick the next block with? Then we can probably give you a little more help.

Share this post


Link to post
Share on other sites
Something like:
number = int( (float)rand() / float(MAX_RANDOM_NUMBER) * float(largest_piece_index) );

This might work, but you have to know that rand() isn''t the best random number to begin with.

You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites
rileyriley: mathematically correct, but the assumption that rand() is random is false. Given the pathetic VC generator (side note: unchanged since qbasic days), whose lower bits are of lesser ''quality'', it is better to use the whole range, as _DarkWIng_ suggests.

Share this post


Link to post
Share on other sites
Just a comment on _DarkWIng_''s suggestion: largest_piece_index would hardly ever be chosen. It would result only if rand() returned exactly MAX_RANDOM_NUMBER. Of course, he did use the phrase "Something like", so he''s forgiven. I''m pretty sure that this will work well enough:

int Random(int Min, int Max)
{
return (int)((double)rand() / ((double)RAND_MAX + 1.0) * (double)(Max - Min + 1)) + Min;
}

rand() / (RAND_MAX + 1) ensures that the resulting fraction is greater than or equal to 0, and almost up to, but not including 1, which is important when returning an evenly distributed integer value.

Share this post


Link to post
Share on other sites

  • 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!