Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A good random number generator for C

Tags:

c

random

I need a good random number generator for a program I'm writing in C. It's a fractal flame generator, if you're interested. My images were coming out very grainy, even though I had success with the same algorithm in the past. The difference, I finally realized, was the random number generator I was using. Incredibly, it makes an ENORMOUS difference. I'm hoping that an even better random number generator might yield better results. The answer could come in the form of a code sample or a link to a pre-existing random number library. The most important requirements:

  • it should produce relatively high quality streams of random numbers
  • its period must be over ten billion
  • it should be fast enough and offer a good performance trade-off.
like image 343
Void Star Avatar asked Feb 15 '13 08:02

Void Star


1 Answers

This seems like a good use-case for the Mersenne Twister

  • It's faster than most standard implementations of rand()
  • It has a very long (2^19937 − 1) period
  • It has a pretty high quality - it passes most standardized randomness tests
  • It's public domain
like image 196
Philipp Avatar answered Sep 21 '22 08:09

Philipp