mardi 23 juin 2020

C++ generate large amounts of random floats(10,000+ numbers) fast

For a project I am currently working on I need to generate a large amount of pseudorandom floating-point numbers, between 0 and 1, in the range of thousands to hundreds of thousands as fast as possible.

What would the best method for this be?

The options I can think of currently are:

C rand(), Quite fast from my experience.

C++11 Random, rather slow but better random numbers from what I have heard.

A custom hash function such as:

u_int32_t seed = 1;
seed = (seed ^ 61) ^ (seed >> 16);
seed = seed + (seed << 3);
seed = seed ^ (seed >> 4);
seed = seed * 0x27d4eb2d;
seed = seed ^ (seed >> 15);
uint8_t a = seed&0xff;
float n = (((float)a)/UINT8_MAX);

Which is quite fast.

Speed test for the above:

Generating 100000 Random Numbers
C rand(): 1.72661
C++ random: 6.60626
hash random: 1.034

Are there any faster options? The only thing I can think of are some quicker hash or somehow doing it on the GPU via a compute shader, but I don't have much experience in that.

Any help is most appreciated.




Aucun commentaire:

Enregistrer un commentaire