Skip to content
Permalink
Browse files
fixes initialization of ISAAC PRNG without explicit seed
adds specs for such initialization
  • Loading branch information
konovod authored and Martin Verzilli committed Aug 28, 2017
1 parent d3537fc commit 98af73959dc7ee5da784b8e0dddd8c33d52fa4f4
Showing 3 changed files with 17 additions and 6 deletions.
@@ -345,4 +345,8 @@ describe "Random::ISAAC" do
m.next_u.should eq(n)
end
end

it "can be initialized without explicit seed" do
Random::ISAAC.new.should be_a Random::ISAAC
end
end
@@ -231,4 +231,8 @@ describe "Random::PCG32" do
m1.jump(-10)
m1.next_u.should eq m2.next_u
end

it "can be initialized without explicit seed" do
Random::PCG32.new.should be_a Random::PCG32
end
end
@@ -12,21 +12,24 @@ class Random::ISAAC
private getter bb
private getter cc

private def self.random_seeds
(uninitialized StaticArray(UInt32, 8)).tap do |seeds|
System::Random.random_bytes(seeds.to_slice)
end
private alias Seeds = StaticArray(UInt32, 8)

private def random_seeds
result = uninitialized Seeds
result_slice = result.unsafe_as(StaticArray(UInt8, sizeof(Seeds))).to_slice
Crystal::System::Random.random_bytes(result_slice)
result
end

def initialize(seeds = self.random_seeds)
def initialize(seeds = random_seeds)
@rsl = StaticArray(UInt32, 256).new { 0_u32 }
@mm = StaticArray(UInt32, 256).new { 0_u32 }
@counter = 0
@aa = @bb = @cc = 0_u32
init_by_array(seeds)
end

def new_seed(seeds = self.random_seeds)
def new_seed(seeds = random_seeds)
@aa = @bb = @cc = 0_u32
init_by_array(seeds)
end

0 comments on commit 98af739

Please sign in to comment.