Randomness in games

Started by
27 comments, last by Vamp355 22 years, 1 month ago
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?
Vamp355, C++ coder in training.
Advertisement
please explain what you mean by "not random enough". give us a sample or something.

- seb
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.
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
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."
------------------------------------------------------------"To a computer, chaos is just another kind of order."
it is unlikely to be zero though
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
I use "Mersenne Twister", it is a 639 dimensional pseudo-random number generator.
And the optimized Version, named cokus, is twice as fast.
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.
using the current time within srand has always provided me with enough randomness
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?

This topic is closed to new replies.

Advertisement