forked from jruby/jruby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JRUBY-6324: random seed for srand is not initialized properly
jruby -e 'p srand' returns previous (initial) srand seed. In 1.8 mode, it always returns 0 but it's not correct. In 1.9 mode, it crashes with NullPointerException because it returns null as RubyObject. Anyway, initial seed value is not initialized properly. This commit includes several fixes for Random. * Initialize the runtime-local PRNG and seed at Ruby#initCore(). * Keep seed for PRNG as not long but RubyInteger. % jruby165 -e 'n = 2**128; srand(n); p [n.object_id, srand.object_id]' [2000, 1] => should be the same object * Introduced Ruby#randomSeedRandom as the runtime-local PRNG for generating seed by srand. Do not use System.currentTimeMillis which is not suited for PRNG seed. * Removed globalRandom in RubyRandom class which is used only in 1.9 mode. Use Ruby#getRandom() for global srand as same as 1.8 mode.
- Loading branch information
Hiroshi Nakamura
committed
Jan 10, 2012
1 parent
69c2b7b
commit f7041c2
Showing
4 changed files
with
62 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'java' | ||
|
||
describe "JRUBY-6324: random seed for srand is not initialized properly" do | ||
it "initializes initial seed for PRNG" do | ||
rt = Java.org.jruby.embed.ScriptingContainer.new | ||
rt.run_scriptlet("srand").should > 0 | ||
end | ||
|
||
it "initializes initial seed for PRNG" do | ||
rt = Java.org.jruby.embed.ScriptingContainer.new | ||
id1, id2 = rt.run_scriptlet("n = 2**128; srand(n); [n.object_id, srand.object_id]") | ||
id1.should == id2 | ||
end | ||
end |
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
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
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