Speed of Random Numbers

matheus matheus at gmail.com
Sat Aug 3 19:31:30 UTC 2019


On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:
> For me the "goodness of random" is NOT important.

If that's the case, you could roll your own RNG:

//DMD64 D Compiler 2.072.2
import std.stdio;
import std.datetime;
import std.array, std.random;

void main(){
     ubyte x;
     auto r = benchmark!(f1,f2)(10_000);
     writeln(r[0]);
     writeln(r[1]);
}

int f1(){
     static s = 10; // Seed
     s = (214013*s+2531011); // [1]
     s = (s>>16)&0x7FFF;
     auto y=(s&7)+1;
     //writeln(y);
     return y;
}

int f2(){
     byte c;
     c=uniform!ubyte() % 8 +1;
     //writeln(c);
     return c;
}


/*
[1] 
https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/
*/

/*
OUTPUT:
TickDuration(65263)   <-f1
TickDuration(635167)  <-f2
*/

Matheus.


More information about the Digitalmars-d-learn mailing list