Class: Random

Inherits:
Object show all
Extended by:
Formatter
Includes:
Formatter
Defined in:
opal/opal/corelib/random.rb,
opal/opal/corelib/random/formatter.rb,
opal/opal/corelib/random/seedrandom.js.rb,
opal/opal/corelib/random/math_random.js.rb,
opal/opal/corelib/random/mersenne_twister.rb

Constant Summary collapse

MATH_RANDOM_GENERATOR =
`{
  new_seed: function() { return 0; },
  reseed: function(seed) { return {}; },
  rand: function($rng) { return Math.random(); }
}`

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.



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

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.



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

def seed
  @seed
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Class Method Details

._verify_count(count) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'opal/opal/corelib/random.rb', line 8

def self._verify_count(count)
  %x{
    if (!$truthy(count)) count = 16;
    if (typeof count !== "number") count = #{`count`.to_int};
    if (count < 0) #{::Kernel.raise ::ArgumentError, 'negative string size (or size too big)'};
    count = Math.floor(count);
    return count;
  }
end

.bytes(length) ⇒ Object



61
62
63
# File 'opal/opal/corelib/random.rb', line 61

def self.bytes(length)
  self::DEFAULT.bytes(length)
end

.generator=(generator) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'opal/opal/corelib/random.rb', line 83

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

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

.new_seedObject



29
30
31
# File 'opal/opal/corelib/random.rb', line 29

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

.rand(limit = undefined) ⇒ Object



33
34
35
# File 'opal/opal/corelib/random.rb', line 33

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

.random_floatObject



79
80
81
# File 'opal/opal/corelib/random.rb', line 79

def self.random_float
  self::DEFAULT.random_float
end

.srand(n = ::Random.new_seed) ⇒ Object



37
38
39
40
41
42
43
# File 'opal/opal/corelib/random.rb', line 37

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

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

.urandom(size) ⇒ Object



45
46
47
# File 'opal/opal/corelib/random.rb', line 45

def self.urandom(size)
  ::SecureRandom.bytes(size)
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
52
53
# File 'opal/opal/corelib/random.rb', line 49

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

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

#bytes(length) ⇒ Object



55
56
57
58
59
# File 'opal/opal/corelib/random.rb', line 55

def bytes(length)
  length = ::Random._verify_count(length)

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

#rand(limit = undefined) ⇒ Object



65
66
67
# File 'opal/opal/corelib/random.rb', line 65

def rand(limit = undefined)
  random_number(limit)
end

#random_floatObject

Not part of the Ruby interface (use #random_number for portability), but used by Random::Formatter as a shortcut, as for Random interface the float RNG is primary.



72
73
74
75
76
77
# File 'opal/opal/corelib/random.rb', line 72

def random_float
  %x{
    self.state++;
    return Opal.$$rand.rand(self.$rng);
  }
end

#reseed(seed) ⇒ Object



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

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