Speed of Random Numbers

Cym13 cpicard at openmailbox.org
Sat Aug 3 17:17:23 UTC 2019


On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:
> Hi to everybody
> I am doing some experiments about random numbers.
> I need "extreme speed" for the generation for numbers from 1 to 
> 8.
>
> Generating 500_000_000 numbers with this code:
>
>
>
> -----------------------------
> import std.stdio, std.array, std.random;
> void main()
> {
>     byte c;
>     writeln("Start");
>     for(int k=1;k<=500_000_000;k++)
>         c=uniform!ubyte() % 8 +1;  //<<< ======= RANDOM
>     writeln("Stop");
> }
> -----------------------------
>
>
>
> I get these results:
>
> c=uniform!ubyte() % 8 +1;  ======>>> Execution time: 15.563 s
> c=cast(byte)uniform(1, 9); ======>>> Execution time: 24.218 s
>
> Do you know other faster functions or methods to generate 
> random numbers?
>
> For me the "goodness of random" is NOT important.
>
> Thank you very much
> GIovanni Di Maria

To what extent isn't the quality of randomness important to you?

Your posts reminds me of the way Doom (the original) did it for 
things like enemy behaviour and shot dispersion: they generated a 
static table of 256 random numbers once and any time they needed 
a random byte they just picked the next in the table. They didn't 
have any security or sciency concern and just wanted to provide a 
different game each time so that worked well for them. You won't 
find anything faster than that I think.


More information about the Digitalmars-d-learn mailing list