Last active
January 25, 2016 06:05
-
-
Save envp/efb0e2f92857c3acef5e to your computer and use it in GitHub Desktop.
Finding the entropy for Ruby's random method as a measure of it's randomness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def rb_rand_entropy(num_trials) | |
| a = Array.new(num_trials) {rand(100)} | |
| p = [] | |
| 0.upto(99) do |i| | |
| p[i] = a.count(i) / num_trials.to_f | |
| end | |
| return p.map {|c| -c * Math.log(c)}.reduce(:+) | |
| end | |
| 1.upto(7) do |t| | |
| puts [Math.exp(rb_rand_entropy(10 ** t)), t].inspect | |
| end | |
| # Returns this (n' is the estimated value of the parameter n, should be 100)\ | |
| # The RHS is (n', num_samples) | |
| # => NaN | |
| # => NaN | |
| # => 4.557993577783632 # => (n', num_samples) = (95.3918912807, 1_000) | |
| # => 4.600420244466219 # => (n', num_samples) = (99.5261321610, 10_000) | |
| # => 4.604752839404147 # => (n', num_samples) = (99.9582740493, 100_000) | |
| # => 4.605116270790972 # => (n', num_samples) = (99.9946086256, 1_000_000) | |
| # => 4.605166004190653 # => (n', num_samples) = (99.9995818211, 10_000_000) | |
| # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment