Created
April 30, 2015 15:31
-
-
Save mrgenixus/656ac17e15282cb01703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'date' | |
| def generate_date do | |
| now = Date.today().to_datetime() | |
| weeks_from_now = Random.rand(8) | |
| day_random = RandomGaussian.new(0.5, 0.25).rand() | |
| day_of_week = (day_random*7+1.5).round() % 7 | |
| time_random = RandomGaussian.new(0.5, 0.1).rand() | |
| time = ((time_random*48+12).round() % 48)/2 | |
| now + weeks_from_now.weeks + day_of_week.days + time.hours | |
| end | |
| class RandomGaussian | |
| def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) | |
| @mean, @sd, @rng = mean, sd, rng | |
| @compute_next_pair = false | |
| end | |
| def rand | |
| if (@compute_next_pair = !@compute_next_pair) | |
| # Compute a pair of random values with normal distribution. | |
| # See http://en.wikipedia.org/wiki/Box-Muller_transform | |
| theta = 2 * Math::PI * @rng.call | |
| scale = @sd * Math.sqrt(-2 * Math.log(1 - @rng.call)) | |
| @g1 = @mean + scale * Math.sin(theta) | |
| @g0 = @mean + scale * Math.cos(theta) | |
| else | |
| @g1 | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment