Skip to content

Commit b3b30eb

Browse files
committed
[GR-19302] Reset the Kernel#rand seed for pre-initialized contexts.
PullRequest: truffleruby/1159
2 parents be190ac + d65a6ce commit b3b30eb

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

spec/ruby/core/kernel/rand_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@
138138
rand(KernelSpecs::CustomRangeFloat.new(1.0)..KernelSpecs::CustomRangeFloat.new(42.0)).should be_an_instance_of(KernelSpecs::CustomRangeFloat)
139139
rand(Time.now..Time.now).should be_an_instance_of(Time)
140140
end
141+
142+
it "is random on boot" do
143+
results = 2.times.map {
144+
out = ruby_exe('p rand', options: '--disable-gems')
145+
Float(out)
146+
}
147+
results.size.should == 2
148+
# this is technically flaky, but very unlikely in a good distribution
149+
results[0].should_not == results[1]
150+
end
141151
end
142152

143153
describe "Kernel#rand" do

spec/tags/core/kernel/rand_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ fails:Kernel.rand given an exclusive range returns a Float between the two given
44
fails:Kernel.rand returns nil when range is backwards
55
fails:Kernel.rand returns the range start/end when Float range is 0
66
fails:Kernel.rand supports custom object types
7+
slow:Kernel.rand is random on boot

src/main/ruby/truffleruby/core/thread.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ def thread_variables
395395
end
396396
end
397397

398-
Thread.current.send :internal_thread_initialize
398+
Truffle::Boot.redo do
399+
Thread.current.send :internal_thread_initialize
400+
end
399401

400402
class ThreadGroup
401403
def initialize

0 commit comments

Comments
 (0)