3 ms·
Whenever I see such libraries, the first thing I check out is their PRNG implementation, and this one must be the worst I've seen so far. (The rest of the libra
by camel-cdr 5y ago
Whenever I see such libraries, the first thing I check out is their PRNG implementation, and this one must be the worst I've seen so far. (The rest of the library is probably quite good, I'm just talking about the PRNG part)
The library has one PRNG, and it is the following:
tb_spinlock_enter(&g_lock);
g_value = (g_value * 10807 + 1) & 0xffffffff;
tb_spinlock_leave(&g_lock);
Using this is literally worse than using the rand reference implementation.
Also, tb_random_range uses a biased and slow implementation with modulo. (see https://www.pcg-random.org/posts/bounded-rands.html https://www.pcg-random.org/posts/bounded-rands.html for the proper way to do this)
I might look into adding a proper PRNG implementation, but I'll recommend a few resources:
A lesson of what not to do: https://youtu.be/LDPMpc-ENqY https://youtu.be/LDPMpc-ENqY ("rand() Considered Harmful")
Some good modern PRNGs: https://www.pcg-random.org/ https://www.pcg-random.org/, https://prng.di.unimi.it/ https://prng.di.unimi.it/, https://romu-random.org/ https://romu-random.org/
Generating random numbers in a specific range: https://www.pcg-random.org/posts/bounded-rands.html https://www.pcg-random.org/posts/bounded-rands.html