Class: Synthra::Generator::RNG
- Inherits:
-
Object
- Object
- Synthra::Generator::RNG
- Defined in:
- lib/synthra/generator/rng.rb
Overview
Thread-safe seeded random number generator
Provides deterministic random number generation using Ruby's Random class with mutex protection for thread safety. All type generators use this RNG to ensure reproducible output.
Instance Attribute Summary collapse
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
Instance Method Summary collapse
-
#boolean(true_probability = 0.5) ⇒ Boolean
Generate a random boolean.
-
#dup ⇒ RNG
Create a duplicate RNG with the same seed.
-
#float(min, max) ⇒ Float
Generate a random float in range.
-
#initialize(seed = nil) ⇒ RNG
constructor
Create a new RNG instance.
-
#int(min, max) ⇒ Integer
Generate a random integer in inclusive range.
-
#rand(max = nil) ⇒ Integer, Float
Generate a random number.
-
#rand_range(range) ⇒ Numeric
Generate a random number in a range (alias).
-
#reset! ⇒ void
Reset the RNG to initial state.
-
#sample(array) ⇒ Object?
Pick a random element from an array.
-
#sample_many(array, count) ⇒ Array
Sample multiple elements from an array.
-
#shuffle(array) ⇒ Array
Shuffle an array.
-
#weighted_choice(options) ⇒ Object?
Select based on weighted probabilities.
Constructor Details
#initialize(seed = nil) ⇒ RNG
Create a new RNG instance
60 61 62 63 64 |
# File 'lib/synthra/generator/rng.rb', line 60 def initialize(seed = nil) @seed = seed || Random.new_seed @random = Random.new(@seed) @mutex = Mutex.new end |
Instance Attribute Details
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
46 47 48 |
# File 'lib/synthra/generator/rng.rb', line 46 def seed @seed end |
Instance Method Details
#boolean(true_probability = 0.5) ⇒ Boolean
Generate a random boolean
174 175 176 |
# File 'lib/synthra/generator/rng.rb', line 174 def boolean(true_probability = 0.5) rand < true_probability end |
#dup ⇒ RNG
Create a duplicate RNG with the same seed
Creates a new RNG that will produce the same sequence.
248 249 250 |
# File 'lib/synthra/generator/rng.rb', line 248 def dup self.class.new(@seed) end |
#float(min, max) ⇒ Float
Generate a random float in range
232 233 234 |
# File 'lib/synthra/generator/rng.rb', line 232 def float(min, max) min + rand * (max - min) end |
#int(min, max) ⇒ Integer
Generate a random integer in inclusive range
217 218 219 |
# File 'lib/synthra/generator/rng.rb', line 217 def int(min, max) rand(min..max) end |
#rand(max = nil) ⇒ Integer, Float
Generate a random number
Flexible random number generation supporting multiple argument types.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/synthra/generator/rng.rb', line 84 def rand(max = nil) @mutex.synchronize do case max when nil @random.rand when Range @random.rand(max) else @random.rand(max) end end end |
#rand_range(range) ⇒ Numeric
Generate a random number in a range (alias)
104 105 106 |
# File 'lib/synthra/generator/rng.rb', line 104 def rand_range(range) rand(range) end |
#reset! ⇒ void
This method returns an undefined value.
Reset the RNG to initial state
Re-initializes the internal random generator to start sequence over.
266 267 268 269 270 |
# File 'lib/synthra/generator/rng.rb', line 266 def reset! @mutex.synchronize do @random = Random.new(@seed) end end |
#sample(array) ⇒ Object?
Pick a random element from an array
118 119 120 121 122 123 124 |
# File 'lib/synthra/generator/rng.rb', line 118 def sample(array) return nil if array.empty? @mutex.synchronize do array.sample(random: @random) end end |
#sample_many(array, count) ⇒ Array
Sample multiple elements from an array
137 138 139 140 141 |
# File 'lib/synthra/generator/rng.rb', line 137 def sample_many(array, count) @mutex.synchronize do array.sample(count, random: @random) end end |
#shuffle(array) ⇒ Array
Shuffle an array
Returns a new shuffled array; does not modify original.
155 156 157 158 159 |
# File 'lib/synthra/generator/rng.rb', line 155 def shuffle(array) @mutex.synchronize do array.shuffle(random: @random) end end |
#weighted_choice(options) ⇒ Object?
Select based on weighted probabilities
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/synthra/generator/rng.rb', line 189 def weighted_choice() return nil if .empty? total = .values.sum.to_f target = rand * total cumulative = 0.0 .each do |value, weight| cumulative += weight return value if target <= cumulative end # Fallback (shouldn't happen but handles float precision) .keys.last end |