假设我有 100 条记录,我想模拟 created_at
日期,以便它符合某些曲线。有没有一个库可以做到这一点,或者我可以使用什么公式?我认为这是沿着相同的轨道:
生成具有概率分布的随机数
我不太了解它们在数学中如何分类,但我正在研究以下内容:
- 钟形曲线
- 对数(典型的生物学/进化)曲线?
...
只是在代码中查找一些公式,这样我就可以这样说:
- 给定 100 条记录,时间跨度为 1.week,间隔为 12.hours,
- 设置<对每条记录进行 code>created_at ,使其大致适合
曲线
非常感谢!
更新
我发现这篇关于 ruby 算法的论坛帖子,其中让我找到了 rsruby,一个 R/Ruby 桥接器,但这似乎太多了。
更新 2
我写了这个小片段,尝试使用 gsl
库,到达那里...
在 Rails 中生成测试数据,其中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:
Generate Random Numbers with Probabilistic Distribution
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?
...
Just looking for some formulas in code so I can say this:
- Given 100 records, a timespan of
1.week
, and an interval of 12.hours
- set
created_at
for each record such that it fits, roughly, to curve
Thanks so much!
Update
I found this forum post about ruby algorithms, which led me to rsruby, an R/Ruby bridge, but that seems like too much.
Update 2
I wrote this little snippet trying out the gsl
library, getting there...
Generate test data in Rails where created_at falls along a Statistical Distribution
发布评论
评论(4)
您可以生成实际上只是整数的 UNIX 时间戳。首先弄清楚你想什么时候开始,例如现在:
找出你的间隔应该什么时候结束(比如 1 周后):
Ruby 使用 此算法来生成随机数。几乎是统一的。然后生成两者之间的随机数:
然后将其转换回日期:
这看起来也很有希望:
http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html
这也是:
http://rb-gsl.rubyforge.org/files/rdoc/rng_rdoc.html
You can generate UNIX timestamps which are really just integers. First figure out when you want to start, for example now:
Find out when the end of your interval should be (say 1 week later):
Ruby uses this algorithm to generate random numbers. It is almost uniform. Then generate random numbers between the two:
Then convert that back to a date:
This looks promising as well:
http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html
And this too:
http://rb-gsl.rubyforge.org/files/rdoc/rng_rdoc.html
来自 wiki:
第一种方法是您SO链接问题中接受的答案中使用的方法: 生成随机数概率分布
From wiki:
The first method is the one used in the accepted answer in your SO linked question: Generate Random Numbers with Probabilistic Distribution
另一个选择是 Distribution gem nofollow">SciRuby。您可以通过以下方式生成正态数字:
还有用于各种其他分布的 RNG。
Another option is the Distribution gem under SciRuby. You can generate normal numbers by:
There are RNGs for various other distributions as well.
我最近遇到了 croupier,这是一个 ruby gem,旨在根据各种统计分布生成数字。
我还没有尝试过,但听起来很有希望。
I recently came across croupier, a ruby gem that aims to generate numbers according to a variety of statistical distributions.
I have yet to try it but it sounds quite promising.