Class: Random

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

Constant Summary

DEFAULT =
new(new_seed)

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



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

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

.new_seedObject



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

def self.new_seed
  %x{
    return Math.abs($seed_generator.int32());
  }
end

.rand(limit = undefined) ⇒ Object



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

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

.srand(n = Random.new_seed) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

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

#bytes(length) ⇒ Object



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

def bytes(length)
  length = Opal.coerce_to!(length, Integer, :to_int)
  length
    .times
    .map { rand(255).chr }
    .join
    .encode(Encoding::ASCII_8BIT)
end

#rand(limit = undefined) ⇒ Object



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
115
116
# File 'opal/opal/corelib/random.rb', line 55

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

    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



12
13
14
15
# File 'opal/opal/corelib/random.rb', line 12

def reseed(seed)
  @seed = seed
  `self.$rng = new Math.seedrandom(seed);`
end