Created
March 20, 2011 03:38
-
-
Save vincentchu/878053 to your computer and use it in GitHub Desktop.
Using Ruby 1.8.7, let's take a look at how the random number generator behaves under forking
This file contains 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
[false, true].each do |reseed_rand| | |
puts "*" * 100 | |
puts "Force reseed of the random number generator? #{reseed_rand}" | |
2.times { | |
fork { | |
srand if reseed_rand | |
puts sprintf(" Process ID: %5d, child of %5d: 3 Randoms: %.5f, %.5f, %.5f", Process.pid, Process.ppid, rand, rand, rand) | |
} | |
} | |
sleep 0.1 | |
end | |
# => | |
# **************************************************************************************************** | |
# Force reseed of the random number generator? false | |
# Process ID: 22572, child of 22571: 3 Randoms: 0.52363, 0.18314, 0.40295 | |
# Process ID: 22573, child of 22571: 3 Randoms: 0.52363, 0.18314, 0.40295 | |
# **************************************************************************************************** | |
# Force reseed of the random number generator? true | |
# Process ID: 22574, child of 22571: 3 Randoms: 0.20881, 0.22412, 0.19271 | |
# Process ID: 22575, child of 22571: 3 Randoms: 0.91050, 0.33603, 0.03308 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment