Class: Random
- 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
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
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- ._verify_count(count) ⇒ Object
- .bytes(length) ⇒ Object
- .generator=(generator) ⇒ Object
- .new_seed ⇒ Object
- .rand(limit = undefined) ⇒ Object
- .random_float ⇒ Object
- .srand(n = ::Random.new_seed) ⇒ Object
- .urandom(size) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bytes(length) ⇒ Object
-
#initialize(seed = ::Random.new_seed) ⇒ Random
constructor
A new instance of Random.
- #rand(limit = undefined) ⇒ Object
-
#random_float ⇒ Object
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.
- #reseed(seed) ⇒ Object
Constructor Details
Instance Attribute Details
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
7 8 9 |
# File 'opal/opal/corelib/random.rb', line 7 def seed @seed end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'opal/opal/corelib/random.rb', line 7 def state @state end |
Class Method Details
._verify_count(count) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'opal/opal/corelib/random.rb', line 9 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
62 63 64 |
# File 'opal/opal/corelib/random.rb', line 62 def self.bytes(length) self::DEFAULT.bytes(length) end |
.generator=(generator) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'opal/opal/corelib/random.rb', line 84 def self.generator=(generator) `Opal.$$rand = #{generator}` if const_defined? :DEFAULT self::DEFAULT.reseed else const_set :DEFAULT, new(new_seed) end end |
.new_seed ⇒ Object
30 31 32 |
# File 'opal/opal/corelib/random.rb', line 30 def self.new_seed `Opal.$$rand.new_seed()` end |
.rand(limit = undefined) ⇒ Object
34 35 36 |
# File 'opal/opal/corelib/random.rb', line 34 def self.rand(limit = undefined) self::DEFAULT.rand(limit) end |
.random_float ⇒ Object
80 81 82 |
# File 'opal/opal/corelib/random.rb', line 80 def self.random_float self::DEFAULT.random_float end |
.srand(n = ::Random.new_seed) ⇒ Object
38 39 40 41 42 43 44 |
# File 'opal/opal/corelib/random.rb', line 38 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
46 47 48 |
# File 'opal/opal/corelib/random.rb', line 46 def self.urandom(size) ::SecureRandom.bytes(size) end |
Instance Method Details
#==(other) ⇒ Object
50 51 52 53 54 |
# File 'opal/opal/corelib/random.rb', line 50 def ==(other) return false unless ::Random === other seed == other.seed && state == other.state end |
#bytes(length) ⇒ Object
56 57 58 59 60 |
# File 'opal/opal/corelib/random.rb', line 56 def bytes(length) length = ::Random._verify_count(length) ::Array.new(length) { rand(255).chr }.join.encode('ASCII-8BIT') end |
#rand(limit = undefined) ⇒ Object
66 67 68 |
# File 'opal/opal/corelib/random.rb', line 66 def rand(limit = undefined) random_number(limit) end |
#random_float ⇒ Object
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.
73 74 75 76 77 78 |
# File 'opal/opal/corelib/random.rb', line 73 def random_float %x{ self.state++; return Opal.$$rand.rand(self.$rng); } end |
#reseed(seed) ⇒ Object
25 26 27 28 |
# File 'opal/opal/corelib/random.rb', line 25 def reseed(seed) @seed = seed `self.$rng = Opal.$$rand.reseed(seed)` end |