Generate Array of Numbers that fit to a Probability Distribution in Ruby?

后端 未结 4 2024
青春惊慌失措
青春惊慌失措 2021-01-03 11:58

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 thi

4条回答
  •  鱼传尺愫
    2021-01-03 12:31

    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
    

    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)
    

    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

提交回复
热议问题