• ### Popular Now

• 10
• 9
• 13
• 10
• 18

#### Archived

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

# Randomness in games

This topic is 5872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
please explain what you mean by "not random enough". give us a sample or something.

- seb

##### Share on other sites
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 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 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 on other sites
it is unlikely to be zero though

##### 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 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 on other sites
using the current time within srand has always provided me with enough randomness