File tree 3 files changed +28
-15
lines changed
3 files changed +28
-15
lines changed Original file line number Diff line number Diff line change @@ -40,19 +40,14 @@ require "random/pcg32"
40
40
module Random
41
41
DEFAULT = PCG32 .new
42
42
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)
51
46
end
52
47
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
56
51
end
57
52
58
53
# Generates a random unsigned integer.
Original file line number Diff line number Diff line change
1
+ require " crystal/system/random"
2
+
1
3
# (c) Bob Jenkins, March 1996, Public Domain
2
4
# You may use this code in any way you wish, and it is free. No warrantee.
3
5
# http://burtleburtle.net/bob/rand/isaacafa.html
@@ -10,15 +12,21 @@ class Random::ISAAC
10
12
private getter bb
11
13
private getter cc
12
14
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)
14
22
@rsl = StaticArray (UInt32 , 256 ).new { 0 _u32 }
15
23
@mm = StaticArray (UInt32 , 256 ).new { 0 _u32 }
16
24
@counter = 0
17
25
@aa = @bb = @cc = 0 _u32
18
26
init_by_array(seeds)
19
27
end
20
28
21
- def new_seed (seeds = StaticArray ( UInt32 , 8 ).new { Random .new_seed } )
29
+ def new_seed (seeds = self .random_seeds )
22
30
@aa = @bb = @cc = 0 _u32
23
31
init_by_array(seeds)
24
32
end
Original file line number Diff line number Diff line change
1
+ require " random/system"
2
+
1
3
# This is a Crystal conversion of basic C PCG implementation
2
4
#
3
5
# Original file notice:
@@ -37,14 +39,22 @@ class Random::PCG32
37
39
@state : UInt64
38
40
@inc : UInt64
39
41
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 )
41
47
# initialize to zeros to prevent compiler complains
42
48
@state = 0 _u64
43
49
@inc = 0 _u64
44
50
new_seed(initstate, initseq)
45
51
end
46
52
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 )
48
58
@state = 0 _u64
49
59
@inc = (initseq << 1 ) | 1
50
60
next_u
You can’t perform that action at this time.
0 commit comments