just might tumble your brains.

Naive calculation of distribution in Ruby

trials = []
10000.times{ trials << rand(6) }
trials.uniq.map do |u|
  trials.select { |t| t.equal? u }.size
end.collect do |c|
  c.to_f / trials.size
end

Here’s a quick-and-dirty calculation of the distribution of values over 10,000 trials of Ruby’s rand() method on the range 0-5, analogous to rolling of a 6-sided die. An example output would be:

#=> [0.1707, 0.161, 0.1695, 0.1654, 0.1659, 0.1675]

It’s not an overly-elegant or efficient implementation. How would you do it?

2 years ago on August 25th, 2009 at 9:22 pm | Permalink
blog comments powered by Disqus