Need better random numbers.

Started by
6 comments, last by olle55 21 years, 7 months ago
Hello. I am writing a tetris game and im an using rand() to select what the next block will be. However if i create new blocks very fast (ex. i hold space down) i often get two or three of the same block in a row, which is kinda boring. So i was wondering if theres any better way to get better random numbers than what im using? This is how i initialize:
  
srand((unsigned)time(NULL));
  
And this is how i select blocks:
  
block = rand() % 7;
  
Thanks in advance!
Advertisement
http://www.agner.org/random/randoma.htm

To fix the double results you could simply check to see if the new random number is equal to the last and if so get another random number. Just do that once to avoid completely removing the possibility of getting the same number twice in a row.

rand() is actually pretty good.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Dot Com | GameShot ]
http://www.boost.org/libs/random/index.html

Plenty of RNG classes with a common interface.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ummmm, if you''re using rand() correctly, then timing should have no bearing on the outputs. Are you re-calling srand() every time you need a number? If so, you shouldn''t -- just call that once at the beginning. Once you''ve done that, the sequence of values you get back from rand() will be identical regardless of the delay between successive calls. And while rand() may not be the best RNG in the world, I highly doubt you need the Mersenne Twister for Tetris. =)

-Brian
the point is with only 7 different numbers the chance to get a double pice is 1:6.
Every so called random function that isn''t able to theoretical produce the same number an infinite times in a row isn''t random...
-----The scheduled downtime is omitted cause of technical problems.
But the Mersenne Twister runs pretty well and is about as good as you''re gonna get
#include < time.h>
#include < cstdlib>
#inclue < iostream>

srand(unsigned(time(0)));

int GetRandom(int min, int max)
{
if(max <0)
max = 0;
if((min==max) && (min==0))
return 0;
return rand()%(max+1-min) +min;
}

cout << "a random number between 0 and 10" << GetRandom(0,10) << "\n";


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."



[edited by - cMADsc on September 9, 2002 11:33:51 AM]

[edited by - cMADsc on September 9, 2002 11:55:40 AM]
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
quote:Original post by OmniBrain
Every so called random function that isn''t able to theoretical produce the same number an infinite times in a row isn''t random...


I think you mean "arbitrarily many" rather than "infinite" here... there''s a big difference!

The first AP was correct. Only call srand once at start-up (or once per level, or whatever). It can help debugging if you DON''T seed to a random value, that way you can record keyboard input and playback the same input & the same random numbers to do a demo, or debugging.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement