Class: RushCheck::StdGen

Inherits:
Object
  • Object
show all
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

TheStdGen

Constant Summary collapse

@@min_bound =
-(2**30)
@@max_bound =
2**30 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#leftObject (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

#rightObject (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_nextObject

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_rangeObject

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

#splitObject

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_sObject



103
104
105
# File 'lib/rushcheck/random.rb', line 103

def to_s
  @left.to_s + ' ' + @right.to_s
end