Random Numbers in UUIDTools
Written March 16th, 2007
I wrote the random number generator for UUIDTools awhile ago, but strangely, I’m only just now getting around to verifying that the numbers it generates really are decently random. Previously, I’d just been “eyeballing” it.
~/Projects/Ruby/Components/uuidtools/lib $ irb
UUIDTools environment loaded.
>> output = File.new("random.txt", "w")
=> #<File:random.txt>
>> 10000.times do
?> output.write(UUID.true_random)
>> end
=> 10000
>> output.close
=> nil
>> exit
~/Projects/Ruby/Components/uuidtools/lib $ ent < random.txt
Entropy = 7.998801 bits per byte.
Optimum compression would reduce the size
of this 160000 byte file by 0 percent.
Chi square distribution for 160000 samples is 266.64, and randomly
would exceed this value 50.00 percent of the times.
Arithmetic mean value of data bytes is 127.1241 (127.5 = random).
Monte Carlo value for Pi is 3.146778669 (error 0.17 percent).
Serial correlation coefficient is -0.000714 (totally uncorrelated = 0.0).
I’m reasonably satisfied with that I think.
By comparison, the builtin rand method in ruby gets:
~/Projects/Ruby/Components/uuidtools/lib $ rm random.txt
~/Projects/Ruby/Components/uuidtools/lib $ irb
UUIDTools environment loaded.
>> output = File.new("random.txt", "w")
=> #<File:random.txt>
>> 160000.times do
?> output.write(rand(256).chr)
>> end
=> 160000
>> output.close
=> nil
>> exit
~/Projects/Ruby/Components/uuidtools/lib $ ent < random.txt
Entropy = 7.998954 bits per byte.
Optimum compression would reduce the size
of this 160000 byte file by 0 percent.
Chi square distribution for 160000 samples is 231.72, and randomly
would exceed this value 75.00 percent of the times.
Arithmetic mean value of data bytes is 127.6154 (127.5 = random).
Monte Carlo value for Pi is 3.150228756 (error 0.27 percent).
Serial correlation coefficient is -0.000933 (totally uncorrelated = 0.0).
And random.org gets:
Entropy = 7.999805 bits per character. Optimum compression would reduce the size of this 1048576 character file by 0 percent. Chi square distribution for 1048576 samples is 283.61, and randomly would exceed this value 25.00 percent of the times. Arithmetic mean value of data bytes is 127.46 (127.5 = random). Monte Carlo value for PI is 3.138961792 (error 0.08 percent). Serial correlation coefficient is 0.000417 (totally uncorrelated = 0.0).
Leave a Response