Class: Random
- Defined in:
- opal/opal/corelib/random.rb,
 opal/opal/corelib/random/seedrandom.js.rb
Constant Summary
- DEFAULT =
- new(new_seed) 
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
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
- #reseed(seed) ⇒ Object
Constructor Details
Instance Attribute Details
#seed ⇒ Object (readonly)
Returns the value of attribute seed
| 4 5 6 | # File 'opal/opal/corelib/random.rb', line 4 def seed @seed end | 
#state ⇒ Object (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_seed ⇒ Object
| 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 | 
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 |