Class: Random

Inherits:
Object show all
Extended by:
Formatter
Includes:
Formatter
Defined in:
opal/opal/corelib/random/formatter.rb,
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.rb

Overview

backtick_javascript: true use_strict: true

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.



20
21
22
23
24
# File 'opal/opal/corelib/random.rb', line 20

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.



8
9
10
# File 'opal/opal/corelib/random.rb', line 8

def seed
  @seed
end

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'opal/opal/corelib/random.rb', line 8

def state
  @state
end

Class Method Details

._verify_count(count) ⇒ Object



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

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



63
64
65
# File 'opal/opal/corelib/random.rb', line 63

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

.generator=(generator) ⇒ Object



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

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



31
32
33
# File 'opal/opal/corelib/random.rb', line 31

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

.rand(limit = undefined) ⇒ Object



35
36
37
# File 'opal/opal/corelib/random.rb', line 35

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

.random_floatObject



81
82
83
# File 'opal/opal/corelib/random.rb', line 81

def self.random_float
  self::DEFAULT.random_float
end

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



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

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



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

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

Instance Method Details

#==(other) ⇒ Object



51
52
53
54
55
# File 'opal/opal/corelib/random.rb', line 51

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

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

#bytes(length) ⇒ Object



57
58
59
60
61
# File 'opal/opal/corelib/random.rb', line 57

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

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

#rand(limit = undefined) ⇒ Object



67
68
69
# File 'opal/opal/corelib/random.rb', line 67

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.



74
75
76
77
78
79
# File 'opal/opal/corelib/random.rb', line 74

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

#reseed(seed) ⇒ Object



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

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