Skip to content

Commit

Permalink
fixes initialization of ISAAC PRNG without explicit seed
Browse files Browse the repository at this point in the history
adds specs for such initialization
  • Loading branch information
konovod authored and Martin Verzilli committed Aug 28, 2017
1 parent d3537fc commit 98af739
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions spec/std/random/isaac_spec.cr
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/std/random/pcg32_spec.cr
Expand Up @@ -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
15 changes: 9 additions & 6 deletions src/random/isaac.cr
Expand Up @@ -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
Expand Down

0 comments on commit 98af739

Please sign in to comment.