Module: RushCheck::HsRandom

Included in:
FalseClass, Float, Integer, String, TrueClass
Defined in:
lib/rushcheck/random.rb

Overview

HsRandom module provides several random number function with the functional random generator. This module is implemented Haskell's System.Random library. This module assumes that the class which includes HsRandom should have an instance method random_range to generate a random number and a new random number generator. It assumes also that the class which includes HsRandom should have a class method bound to give a bound of random numbers.

Instance Method Summary collapse

Instance Method Details

#random(gen, lo = nil, hi = nil) ⇒ Object

random requires the functional random number generater (StdGen object) and optionally requires the bound of random numbers. It returns an array with length 2, where the first component should be a new random number, and the last should be a new random number generator.



171
172
173
174
175
176
177
# File 'lib/rushcheck/random.rb', line 171

def random(gen, lo=nil, hi=nil)
  lo = bound[0] if lo.nil?
  hi = bound[1] if hi.nil?
  random_range(gen, hi, lo) if lo > hi

  random_range(gen, lo, hi)
end

#random_array(gen, len = nil, lo = nil, hi = nil) ⇒ Object

random_array requires the functional random number generater (StdGen object). Optionally, it requires the length of results and the bound of random numbers. This method returns different result whether the second argument length is nil or not. When the second argument is nil, then random_array returns a Proc which takes one variable as an integer and return a new random value, such as an infinite sequence of random numbers. Otherwise, the second argument of random_array is not nil but some integer, then random_array returns an array of random numbers with the length.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rushcheck/random.rb', line 188

def random_array(gen, len=nil, lo=nil, hi=nil)
  g = gen
  if len.nil?
  then
    Proc.new do |i|
      (i+1).times do
        v, g = random(g, lo, hi)
      end
      v
    end
  else
    (1..len).map do
      v, g = random(g, lo, hi)
      v
    end
  end
end

#random_std(lo = nil, hi = nil) ⇒ Object

random_std requires optionally the bound of random numbers. It returns an array with length 2, where the first component should be a new random number, and the last should be a new random number generator. This method uses the unique standard random generator TheStdGen.



211
212
213
# File 'lib/rushcheck/random.rb', line 211

def random_std(lo=nil, hi=nil)
  random(RushCheck::TheStdGen.instance, lo, hi)
end