Skip to content

Commit d638238

Browse files
konovodRX14
authored andcommittedAug 14, 2017
seed PRNGs from System source instead of time. (#4789)
1 parent ac88068 commit d638238

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed
 

Diff for: ‎src/random.cr

+6-11
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,14 @@ require "random/pcg32"
4040
module Random
4141
DEFAULT = PCG32.new
4242

43-
# Returns a `UInt32` read from a counter value generated by the cycle counter
44-
# register, or the current time on ARM processors.
45-
def self.new_seed : UInt32
46-
{% if flag?(:arm) || flag?(:aarch64) %}
47-
Time.now.ticks.to_u32
48-
{% else %}
49-
Intrinsics.read_cycle_counter.to_u32
50-
{% end %}
43+
# Initializes an instance with the given *seed* and *sequence*.
44+
def self.new(seed, sequence = 0_u64)
45+
PCG32.new(seed.to_u64, sequence)
5146
end
5247

53-
# Initializes an instance with the given *seed*. (Default: `#new_seed`)
54-
def self.new(seed = new_seed)
55-
PCG32.new(seed)
48+
# Initializes an instance seeded from a system source.
49+
def self.new
50+
PCG32.new
5651
end
5752

5853
# Generates a random unsigned integer.

Diff for: ‎src/random/isaac.cr

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "crystal/system/random"
2+
13
# (c) Bob Jenkins, March 1996, Public Domain
24
# You may use this code in any way you wish, and it is free. No warrantee.
35
# http://burtleburtle.net/bob/rand/isaacafa.html
@@ -10,15 +12,21 @@ class Random::ISAAC
1012
private getter bb
1113
private getter cc
1214

13-
def initialize(seeds = StaticArray(UInt32, 8).new { Random.new_seed })
15+
private def self.random_seeds
16+
(uninitialized StaticArray(UInt32, 8)).tap do |seeds|
17+
System::Random.random_bytes(seeds.to_slice)
18+
end
19+
end
20+
21+
def initialize(seeds = self.random_seeds)
1422
@rsl = StaticArray(UInt32, 256).new { 0_u32 }
1523
@mm = StaticArray(UInt32, 256).new { 0_u32 }
1624
@counter = 0
1725
@aa = @bb = @cc = 0_u32
1826
init_by_array(seeds)
1927
end
2028

21-
def new_seed(seeds = StaticArray(UInt32, 8).new { Random.new_seed })
29+
def new_seed(seeds = self.random_seeds)
2230
@aa = @bb = @cc = 0_u32
2331
init_by_array(seeds)
2432
end

Diff for: ‎src/random/pcg32.cr

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "random/system"
2+
13
# This is a Crystal conversion of basic C PCG implementation
24
#
35
# Original file notice:
@@ -37,14 +39,22 @@ class Random::PCG32
3739
@state : UInt64
3840
@inc : UInt64
3941

40-
def initialize(initstate = UInt64.new(Random.new_seed), initseq = 0_u64)
42+
def self.new
43+
new(Random::System.rand(UInt64::MIN..UInt64::MAX), Random::System.rand(UInt64::MIN..UInt64::MAX))
44+
end
45+
46+
def initialize(initstate : UInt64, initseq = 0_u64)
4147
# initialize to zeros to prevent compiler complains
4248
@state = 0_u64
4349
@inc = 0_u64
4450
new_seed(initstate, initseq)
4551
end
4652

47-
def new_seed(initstate = UInt64.new(Random.new_seed), initseq = 0_u64)
53+
def new_seed
54+
new_seed(Random::System.rand(UInt64::MIN..UInt64::MAX), Random::System.rand(UInt64::MIN..UInt64::MAX))
55+
end
56+
57+
def new_seed(initstate : UInt64, initseq = 0_u64)
4858
@state = 0_u64
4959
@inc = (initseq << 1) | 1
5060
next_u

0 commit comments

Comments
 (0)
Please sign in to comment.