生成适合于Ruby的一个概率分布数值数组? [英] Generate Array of Numbers that fit to a Probability Distribution in Ruby?
问题描述
说我有100条记录,我想模拟出的 created_at
更新,以便它的一些曲线拟合。有一个库来做到这一点,我也可以用什么公式?我认为这是沿着同样的轨迹:
Say I have 100 records, and I want to mock out the created_at
date so it fits on some curve. Is there a library to do that, or what formula could I use? I think this is along the same track:
<一个href="http://stackoverflow.com/questions/3109670/generate-random-numbers-with-probabilistic-distribution">Generate随机数与概率分布
我不知道很多关于他们是如何划分的数学,但我正在寻找的东西,如:
I don't know much about how they are classified in mathematics, but I'm looking at things like:
- 钟形曲线
- 在对数(典型的生物/进化)曲线? ...
- bell curve
- logarithmic (typical biology/evolution) curve? ...
只是为了寻找在code一些公式,所以我可以这样说:
Just looking for some formulas in code so I can say this:
- 有了100条记录,
1.week
的时间跨度,而12.hours
- 设置
created_at
的每条记录,使得它适合,粗略来算,曲线
- Given 100 records, a timespan of
1.week
, and an interval of12.hours
- set
created_at
for each record such that it fits, roughly, tocurve
太感谢了!
更新
我发现这个论坛的帖子关于Ruby算法的,这使我的 rsruby ,一个R / Ruby的桥梁,但是,似乎是太多了。
I found this forum post about ruby algorithms, which led me to rsruby, an R/Ruby bridge, but that seems like too much.
更新2
我写了这个小片段在试用 GSL
库,到那里......
I wrote this little snippet trying out the gsl
library, getting there...
生成Rails中的测试数据,其中created_at沿下降的统计分布
推荐答案
您可以生成UNIX时间戳这是真的只是整数。首先要弄清楚的时候,你想创业,例如现在:
You can generate UNIX timestamps which are really just integers. First figure out when you want to start, for example now:
start = DateTime::now().to_time.to_i
了解,当你的间隔到底应该(比如1周后):
Find out when the end of your interval should be (say 1 week later):
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 the two:
r = Random.new.rand(start..finish)
然后再转换回日期:
Then convert that back to a date:
d = Time.at(r)
这看起来很有希望,以及: http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html
This looks promising as well: http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html
和这一点: http://rb-gsl.rubyforge.org/files/rdoc/rng_rdoc.html
这篇关于生成适合于Ruby的一个概率分布数值数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!