get a random number from a normal distribution with a mean of 0 and a standard deviation of 1 in ruby

To generate a random number from a normal distribution with a mean of 0 and a standard deviation of 1 in Ruby, we can use the rand method from the Distribution module of the ruby-statistics gem. Here's how:

main.rb
require 'statistics2'

# generate a random number from a normal distribution with mean 0 and standard deviation 1
random_number = Statistics2.rnorm(0, 1)

# print the result
puts random_number
193 chars
8 lines

In this code, we first require the statistics2 gem. We then use the rnorm method to generate a random number from a normal distribution with mean 0 and standard deviation 1. We pass the mean and standard deviation as the first and second argument respectively.

The rnorm method returns a single random number.

We then print the result using the puts method.

gistlibby LogSnag