Speed of Random Numbers

Dennis dkorpel at gmail.com
Sat Aug 3 19:19:43 UTC 2019


On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:
> Do you know other faster functions or methods to generate 
> random numbers?
>
> For me the "goodness of random" is NOT important.

I found some nice random functions in this public-domain C 
single-header library collection, one of which is GameRand:

https://github.com/mattiasgustavsson/libs/blob/022370a79cf2d5f87fb43b420834a069adb5fede/rnd.h#L449

Here's the D version:
```
struct GameRand
{
	uint[2] state;
}

uint randomGameRand(ref GameRand gamerand) {
     gamerand.state[0] = ( gamerand.state[0] << 16 ) + ( 
gamerand.state[0] >> 16 );
     gamerand.state[0] += gamerand.state[1];
     gamerand.state[1] += gamerand.state[0];
     return gamerand.state[0];
}
```

It's really fast and decent enough for games (hence the name I 
suppose). See:
http://www.flipcode.com/archives/07-15-2002.shtml


More information about the Digitalmars-d-learn mailing list