Create an array of numbers that matches the probability distribution in Ruby?

Let's say I have 100 entries and I want to make fun of the created_at date so that it matches some curve. Is there a library for this or what formula can I use? I think this is the same track:

Generate random numbers with probability distribution

I don't know much about how they are classified in mathematics, but I look at things like:

  • bell curve
  • logarithmic (typical biology / evolution) curve? ...

Just find some formulas in the code so I can say the following:

  • Given 100 entries, the time interval is 1.week and the interval is 12.hours
  • set created_at for each record so that it approaches curve

Many thanks!

Update

I found this post in the ruby โ€‹โ€‹algorithms forum , which led me to rsruby , R / Ruby bridge, but it seems too big.

Update 2

I wrote this little snippet, having tested the gsl library, getting there ...

Generate test data in Rails, where created_at falls along the statistical distribution

+2
source share
4 answers

You can create UNIX timestamps that are really integers. First find out when you want to start, for example, now:

 start = DateTime::now().to_time.to_i 

Find out when your interval ends (say, after 1 week):

 finish = (DateTime::now()+1.week).to_time.to_i 

Ruby uses this algorithm to generate random numbers. It is almost uniform. Then generate random numbers between them:

 r = Random.new.rand(start..finish) 

Then convert this back to date:

 d = Time.at(r) 

This looks promising: http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html

And this too: http://rb-gsl.rubyforge.org/files/rdoc/rng_rdoc.html

+2
source

I recently met croupier , a ruby โ€‹โ€‹pearl whose purpose is to generate numbers according to a multitude of statistical distributions.

I still have to try, but that sounds pretty promising.

+3
source

From the wiki :

There are several ways: generate a random number based on a probability density function. These methods include converting a uniform random number in some way. Because of this, these methods work equally well in generating both pseudo-random and true random numbers.

One method, called the inversion method , involves integrating up to a region greater than or equal to a random number (which must be generated between 0 and 1 for the correct distribution).

The second method, called the accept-reject method , involves selecting the values โ€‹โ€‹of x and y and checking whether the function x is greater than the value of y. If so, the value of x is accepted. Otherwise, the x value is rejected, and the algorithm tries again.

The first method is the one used in the accepted answer in your SO related question: Generate random numbers with probability distribution

0
source

Another option is a Distribution gem under SciRuby . You can generate normal numbers:

 require 'distribution' rng = Distribution::Normal.rng random_numbers = Array.new(100).map { rng.call } 

There are also RNGs for various other distributions.

0
source

All Articles