Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes initialization of ISAAC PRNG without explicit seed #4882

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/std/random/isaac_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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