Archived

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

Need better random numbers.

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

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!

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
But the Mersenne Twister runs pretty well and is about as good as you''re gonna get

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites