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:
created_at
Generate random numbers with probability distribution
I don't know much about how they are classified in mathematics, but I look at things like:
Just find some formulas in the code so I can say the following:
1.week
12.hours
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 ...
gsl
Generate test data in Rails, where created_at falls along the statistical distribution
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
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.
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.
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
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.