Skip to content

Commit 98af739

Browse files
konovodMartin Verzilli
authored and
Martin Verzilli
committedAug 28, 2017
fixes initialization of ISAAC PRNG without explicit seed
adds specs for such initialization
1 parent d3537fc commit 98af739

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed
 

‎spec/std/random/isaac_spec.cr

+4
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,8 @@ describe "Random::ISAAC" do
345345
m.next_u.should eq(n)
346346
end
347347
end
348+
349+
it "can be initialized without explicit seed" do
350+
Random::ISAAC.new.should be_a Random::ISAAC
351+
end
348352
end

‎spec/std/random/pcg32_spec.cr

+4
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,8 @@ describe "Random::PCG32" do
231231
m1.jump(-10)
232232
m1.next_u.should eq m2.next_u
233233
end
234+
235+
it "can be initialized without explicit seed" do
236+
Random::PCG32.new.should be_a Random::PCG32
237+
end
234238
end

‎src/random/isaac.cr

+9-6
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ class Random::ISAAC
1212
private getter bb
1313
private getter cc
1414

15-
private def self.random_seeds
16-
(uninitialized StaticArray(UInt32, 8)).tap do |seeds|
17-
System::Random.random_bytes(seeds.to_slice)
18-
end
15+
private alias Seeds = StaticArray(UInt32, 8)
16+
17+
private def random_seeds
18+
result = uninitialized Seeds
19+
result_slice = result.unsafe_as(StaticArray(UInt8, sizeof(Seeds))).to_slice
20+
Crystal::System::Random.random_bytes(result_slice)
21+
result
1922
end
2023

21-
def initialize(seeds = self.random_seeds)
24+
def initialize(seeds = random_seeds)
2225
@rsl = StaticArray(UInt32, 256).new { 0_u32 }
2326
@mm = StaticArray(UInt32, 256).new { 0_u32 }
2427
@counter = 0
2528
@aa = @bb = @cc = 0_u32
2629
init_by_array(seeds)
2730
end
2831

29-
def new_seed(seeds = self.random_seeds)
32+
def new_seed(seeds = random_seeds)
3033
@aa = @bb = @cc = 0_u32
3134
init_by_array(seeds)
3235
end

0 commit comments

Comments
 (0)
Please sign in to comment.