Class: Random

Inherits:
Object show all
Defined in:
opal/opal/corelib/random.rb,
opal/opal/corelib/random/seedrandom.js.rb,
opal/opal/corelib/random/math_random.js.rb,
opal/opal/corelib/random/mersenne_twister.js.rb

Constant Summary

SEEDRANDOM_GENERATOR =
`{
  new_seed: function() { return Math.abs($seed_generator.int32()); },
  reseed: function(seed) { return new seedrandom(seed); },
  rand: function($rng) { return $rng.quick(); }
}`
MATH_RANDOM_GENERATOR =
`{
  new_seed: function() { return 0; },
  reseed: function(seed) { return {}; },
  rand: function($rng) { return Math.random(); }
}`
MERSENNE_TWISTER_GENERATOR =
`{
  new_seed: function() { return Math.round(Math.random() * MAX_INT); },
  reseed: function(seed) { return MersenneTwister.init(seed); },
  rand: function(mt) { return MersenneTwister.genrand_real(mt); }
}`

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed = Random.new_seed) ⇒ Random

Returns a new instance of Random



4
5
6
7
8
# File 'opal/opal/corelib/random.rb', line 4

def initialize(seed = Random.new_seed)
  seed = Opal.coerce_to!(seed, Integer, :to_int)
  @state = seed
  reseed(seed)
end

Instance Attribute Details

#seedObject (readonly)

Returns the value of attribute seed



2
3
4
# File 'opal/opal/corelib/random.rb', line 2

def seed
  @seed
end

#stateObject (readonly)

Returns the value of attribute state



2
3
4
# File 'opal/opal/corelib/random.rb', line 2

def state
  @state
end

Class Method Details

.generator=(generator) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'opal/opal/corelib/random.rb', line 116

def self.generator=(generator)
  `Opal.$$rand = #{generator}`

  if const_defined? :DEFAULT
    DEFAULT.reseed
  else
    const_set :DEFAULT, new(new_seed)
  end
end

.new_seedObject



15
16
17
# File 'opal/opal/corelib/random.rb', line 15

def self.new_seed
  `Opal.$$rand.new_seed()`
end

.rand(limit = undefined) ⇒ Object



19
20
21
# File 'opal/opal/corelib/random.rb', line 19

def self.rand(limit = undefined)
  DEFAULT.rand(limit)
end

.srand(n = Random.new_seed) ⇒ Object



23
24
25
26
27
28
29
# File 'opal/opal/corelib/random.rb', line 23

def self.srand(n = Random.new_seed)
  n = Opal.coerce_to!(n, Integer, :to_int)

  previous_seed = DEFAULT.seed
  DEFAULT.reseed(n)
  previous_seed
end

.urandom(size) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'opal/opal/corelib/random.rb', line 31

def self.urandom(size)
  size = Opal.coerce_to!(size, Integer, :to_int)

  if size < 0
    raise ArgumentError, 'negative string size (or size too big)'
  end

  Array.new(size) { rand(255).chr }.join.encode('ASCII-8BIT')
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
44
45
# File 'opal/opal/corelib/random.rb', line 41

def ==(other)
  return false unless Random === other

  seed == other.seed && state == other.state
end

#bytes(length) ⇒ Object



47
48
49
50
51
# File 'opal/opal/corelib/random.rb', line 47

def bytes(length)
  length = Opal.coerce_to!(length, Integer, :to_int)

  Array.new(length) { rand(255).chr }.join.encode('ASCII-8BIT')
end

#rand(limit = undefined) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'opal/opal/corelib/random.rb', line 53

def rand(limit = undefined)
  %x{
    function randomFloat() {
      self.state++;
      return Opal.$$rand.rand(self.$rng);
    }

    function randomInt() {
      return Math.floor(randomFloat() * limit);
    }

    function randomRange() {
      var min = limit.begin,
          max = limit.end;

      if (min === nil || max === nil) {
        return nil;
      }

      var length = max - min;

      if (length < 0) {
        return nil;
      }

      if (length === 0) {
        return min;
      }

      if (max % 1 === 0 && min % 1 === 0 && !limit.excl) {
        length++;
      }

      return self.$rand(length) + min;
    }

    if (limit == null) {
      return randomFloat();
    } else if (limit.$$is_range) {
      return randomRange();
    } else if (limit.$$is_number) {
      if (limit <= 0) {
        #{raise ArgumentError, "invalid argument - #{limit}"}
      }

      if (limit % 1 === 0) {
        // integer
        return randomInt();
      } else {
        return randomFloat() * limit;
      }
    } else {
      limit = #{Opal.coerce_to!(limit, Integer, :to_int)};

      if (limit <= 0) {
        #{raise ArgumentError, "invalid argument - #{limit}"}
      }

      return randomInt();
    }
  }
end

#reseed(seed) ⇒ Object



10
11
12
13
# File 'opal/opal/corelib/random.rb', line 10

def reseed(seed)
  @seed = seed
  `self.$rng = Opal.$$rand.reseed(seed)`
end