   Generating evenly distributed random numbers.
  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.ThanksDarkStarUK   -------------------------------Loves cross-posting because it works 
  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 ;  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. ;  read up on boost.random ;  I hope you''re not making something likeif (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 ;  Actually, if you read up on it, using the modulous operator (%) does not distribute the numbers evenly.   daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool ;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. ;  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. ;  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. ;  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. 