Jump to content
  • Advertisement

Archived

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

Vamp355

Randomness in games

This topic is 5966 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, I am working on a game using some dice, but the Rand function, even when seeded with Srand, just doesn''t seem random enough. I have heard of a better function to do the random number generation, but nobody I know, knows what it is. Can anyone help me?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
as far as i know the standard rand() function should provide a good distribution. though you can easily implement your own using either "linear" or "additive congruence", or try a search for "mersenne twister", which provides very good random results at good speed.

Share this post


Link to post
Share on other sites
rand() isn't that bad. Some ideas on how to improve it:

result = rand() * rand() * rand() + rand() (this will wrap around on 32bit, but will be slow)

Or use a hashing table:

  
static short HashTab[1024];

init()
{
for(i=0;i<1024;i++) HashTab[i] = rand();
}

short random()
{
int p = rand()&1023; // get random table index

int r = HashTab[p]; // result value

HashTab[p] = rand(); // update table entry

return( r );
}


the hashing thing works rather well for me. And there are lots of more complex hashing functions (for eg. encryption), but for a simple game rand(), this simple one should be enough.

[Edit: forum tags don't like me...]


Edited by - Yann L on February 19, 2002 11:58:50 AM

Share this post


Link to post
Share on other sites
I think that

result = rand() * rand() * rand() + rand();

is a complete waste of cpu cycles beause if only one of the first three rand() calls equals to zero the result will be the same as result = rand(); but it will have taken 4 times as many cycles...

------------------------------------------------------------
"To a computer, chaos is just another kind of order."

Share this post


Link to post
Share on other sites
I use "Mersenne Twister", it is a 639 dimensional pseudo-random number generator.
And the optimized Version, named cokus, is twice as fast.

Share this post


Link to post
Share on other sites
quote:

is a complete waste of cpu cycles beause if only one of the first three rand() calls equals to zero the result will be the same as result = rand(); but it will have taken 4 times as many cycles...



Well, it''s an accepted fact in digital signal processing, that if you combine (multiply, add, whatever) two unrelated noise signals, the result will be even more random than before. So it''s definitely not a waste of cycles, if you''re after good randomness. It''s a not so good solution, because to be optimal, the noise sources would need to be unrelated, and that is definitely not the case. But it''s simple and quick to implement. I have the source to a blazing fast hashing PRNG, with a distribution that makes it suitable for very strong encryption. This is a totally different league, of course, but it''s very complex to implement, and takes a lot of RAM (for tables). And the initialization takes up to 30 seconds, because it uses harddisk access timing as noise source to initialize the table pointers. It''s always a question of want your needs are. For a simple dice game, it''s a definitely a bit overkill... I think my hashing function above would be suitable for that kind of task.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why does this question come up so often? What are you trying to do that requires something so random that, at the moment, the user can tell it isn''t?

What evidence have you got that your random dice throws are not random enough?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!