Random Number Generator: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
m →‎Sampling manually: produces -> produce (typo)
→‎Rolling your own: Add a TestU01 example
Line 242:
=== Rolling your own ===
 
If you are still not satisfied, you may want to build your own PRNG. You can do this in a fairly trial-and-error way by writing something that looks good and subjecting it to automated statistical testing suites such as [http://simul.iro.umontreal.ca/testu01/tu01.html TestU01]'s SmallCrush, Crush and finally BigCrush. These tests are very easy to run and can quickly point out problems, for example:
 
<source lang="c">
#include <unif01.h>
#include <bbattery.h>
 
#include <stdint.h>
 
static uint32_t next = 1;
unsigned int custom_rand(void) {
// This is a terrible generator!
next = (next * 0x4B4B9656U) ^ (next * 0x565AC3C3) + 1;
return next * (next - 1);
}
 
int main(void) {
unif01_Gen *gen = unif01_CreateExternGenBits("custom_rand", custom_rand);
bbattery_SmallCrush(gen); // or bbattery_Crush or bbattery_BigCrush
unif01_DeleteExternGenBits(gen);
}
</source>
 
SmallCrush will report that this generator failed 12 out of 15 statistical tests. The other tests, which are also much slower, are therefore not necessary.
 
[[Category:Common Algorithms]]