Class: RushCheck::StdGen
- Inherits:
-
Object
- Object
- RushCheck::StdGen
- Includes:
- RandomGen
- Defined in:
- lib/rushcheck/random.rb
Overview
StdGen is a class (and the unique class) which includes RandomGen. This class provides a functional random number generator.
Direct Known Subclasses
Constant Summary collapse
- @@min_bound =
-(2**30)
- @@max_bound =
2**30 - 1
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
left and right are two seeds of random number generator.
-
#right ⇒ Object
readonly
left and right are two seeds of random number generator.
Instance Method Summary collapse
-
#gen_next ⇒ Object
gen_next returns an array with length 2.
-
#gen_range ⇒ Object
gen_range returns an array with length 2 which represents a bound of generated random numbers.
-
#initialize(left = nil, right = nil) ⇒ StdGen
constructor
A new instance of StdGen.
-
#split ⇒ Object
split returns an array with length 2.
- #to_s ⇒ Object
Constructor Details
#initialize(left = nil, right = nil) ⇒ StdGen
Returns a new instance of StdGen.
69 70 71 72 73 74 75 76 |
# File 'lib/rushcheck/random.rb', line 69 def initialize(left=nil, right=nil) @left, @right = [left, right].map do |x| if x.nil? then random else in_range(x) end end end |
Instance Attribute Details
#left ⇒ Object (readonly)
left and right are two seeds of random number generator.
68 69 70 |
# File 'lib/rushcheck/random.rb', line 68 def left @left end |
#right ⇒ Object (readonly)
left and right are two seeds of random number generator.
68 69 70 |
# File 'lib/rushcheck/random.rb', line 68 def right @right end |
Instance Method Details
#gen_next ⇒ Object
gen_next returns an array with length 2. The first component of result is a random integer. The last component is a new StdGen object as a new random number generator. See also RandomGen.
81 82 83 84 85 86 |
# File 'lib/rushcheck/random.rb', line 81 def gen_next s, t = [@left, @right].map {|x| random(x) } z = ((s - t) % (@@max_bound - @@min_bound)) + @@min_bound [z, RushCheck::StdGen.new(s, t)] end |
#gen_range ⇒ Object
gen_range returns an array with length 2 which represents a bound of generated random numbers.
99 100 101 |
# File 'lib/rushcheck/random.rb', line 99 def gen_range [@@min_bound, @@max_bound] end |
#split ⇒ Object
split returns an array with length 2. The components are two new StdGen objects as two new random number generators.
90 91 92 93 94 95 |
# File 'lib/rushcheck/random.rb', line 90 def split g = gen_next[1] s, t = g.left, g.right [RushCheck::StdGen.new(s + 1, t), RushCheck::StdGen.new(s, t + 1)] end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/rushcheck/random.rb', line 103 def to_s @left.to_s + ' ' + @right.to_s end |