Class: AppMath::Ran

Inherits:
Object
  • Object
show all
Defined in:
lib/random.rb

Overview

Class of random generators. The real numbers delivered by different random generators are intended to be statistically independent. Each random generator has its own interval within which the output values are uniformly distributed.

Constant Summary collapse

@@seed =
137
@@swing =
133216711

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arg) ⇒ Ran

The arguments have to determine an interval in which the random values lie. This is named @iv. If there is no argument we have @iv = [0,1]. If there is one argument, it has to be an instance of Iv and then becomes @iv. If there are two arguments they need to be convertible to R and then become the boundaries of @iv.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/random.rb', line 51

def initialize(*arg)
  n = arg.size
  case n
  when 0
    @iv = Iv.new(0,1)
  when 1
    a0 = arg[0]
    fail "argument must be an Iv" unless a0.class == Iv
    @iv = a0
  when 2
    a0 = arg[0]; a1 = arg[1]
    @iv = Iv.new(a0,a1)
  else
    fail "0,1,or 2 arguments needed, not #{n}"
  end
  x = R.ran(@@seed)
  i = (@@swing * x).to_i
  @local_seed = i
  @@seed += 1
end

Instance Attribute Details

#ivObject (readonly)

Returns the value of attribute iv.



42
43
44
# File 'lib/random.rb', line 42

def iv
  @iv
end

Instance Method Details

#local_seedObject

Returns the local seed for control purposes. Should not be needed.



82
83
84
# File 'lib/random.rb', line 82

def local_seed
  @local_seed
end

#ranObject

Returns a point (value) from interval @iv by ‘random selection’. The underlying mechanism is the sine-floor generator defined in method R.ran.



75
76
77
78
79
# File 'lib/random.rb', line 75

def ran
  p = R.ran(@local_seed)
  @local_seed += 1
  @iv.put(p)
end