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

Overview

helpers: falsy

Defined Under Namespace

Modules: Formatter

Constant Summary collapse

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 mersenne_twister.init(seed); },
  rand: function(mt) { return mersenne_twister.genrand_real(mt); }
}`

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatter

alphanumeric, base64, hex, random_bytes, random_number, urlsafe_base64, uuid

Constructor Details

#initialize(seed = Random.new_seed) ⇒ Random

Returns a new instance of Random.



16
17
18
19
20
# File 'opal/opal/corelib/random.rb', line 16

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.



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

def seed
  @seed
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Class Method Details

._verify_count(count) ⇒ Object



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

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

.bytes(length) ⇒ Object



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

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

.generator=(generator) ⇒ Object



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

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

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

.new_seedObject



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

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

.rand(limit = undefined) ⇒ Object



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

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

.random_floatObject



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

def self.random_float
  DEFAULT.random_float
end

.srand(n = Random.new_seed) ⇒ Object



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

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



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

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

#bytes(length) ⇒ Object



53
54
55
56
57
# File 'opal/opal/corelib/random.rb', line 53

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

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

#rand(limit = undefined) ⇒ Object



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

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.



70
71
72
73
74
75
# File 'opal/opal/corelib/random.rb', line 70

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

#reseed(seed) ⇒ Object



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

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