Class: Numo::Random::Generator
- Inherits:
-
Object
- Object
- Numo::Random::Generator
- Defined in:
- lib/numo/random/generator.rb
Overview
Generator is a class that generates random number with several distributions.
Instance Attribute Summary collapse
-
#algorithm ⇒ String
Returns random number generation algorithm.
Instance Method Summary collapse
-
#cauchy(shape:, loc: 0.0, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Cauchy (Lorentz) distribution.
-
#chisquare(shape:, df:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Chi-squared distribution.
-
#discrete(shape:, weight:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random integer values in the interval [0, n).
-
#f(shape:, dfnum:, dfden:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the F-distribution.
-
#initialize(seed: nil, algorithm: 'pcg64') ⇒ Generator
constructor
Creates a new random number generator.
-
#lognormal(shape:, mean: 0.0, sigma: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to a log-normal distribution.
-
#normal(shape:, loc: 0.0, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to a normal (Gaussian) distribution.
-
#random ⇒ Float
Returns random number with uniform distribution in the half-open interval [0, 1).
-
#seed ⇒ Integer
Returns the seed of random number generator.
-
#seed=(val) ⇒ Object
Sets the seed of random number generator.
-
#standard_t(shape:, df:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Student’s t-distribution.
-
#uniform(shape:, low: 0.0, high: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of uniformly distributed random values in the interval [low, high).
Constructor Details
#initialize(seed: nil, algorithm: 'pcg64') ⇒ Generator
Creates a new random number generator.
28 29 30 31 |
# File 'lib/numo/random/generator.rb', line 28 def initialize(seed: nil, algorithm: 'pcg64') # rubocop:disable Lint/UnusedMethodArgument @algorithm = 'pcg64' @rng = PCG64.new(seed: seed) end |
Instance Attribute Details
#algorithm ⇒ String
Returns random number generation algorithm.
22 23 24 |
# File 'lib/numo/random/generator.rb', line 22 def algorithm @algorithm end |
Instance Method Details
#cauchy(shape:, loc: 0.0, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Cauchy (Lorentz) distribution.
118 119 120 121 122 |
# File 'lib/numo/random/generator.rb', line 118 def cauchy(shape:, loc: 0.0, scale: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.cauchy(x, loc: loc, scale: scale) x end |
#chisquare(shape:, df:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Chi-squared distribution.
136 137 138 139 140 |
# File 'lib/numo/random/generator.rb', line 136 def chisquare(shape:, df:, dtype: :float64) x = klass(dtype).new(shape) rng.chisquare(x, df: df) x end |
#discrete(shape:, weight:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random integer values in the interval [0, n).
80 81 82 83 84 |
# File 'lib/numo/random/generator.rb', line 80 def discrete(shape:, weight:, dtype: :int32) x = klass(dtype).new(shape) rng.discrete(x, weight: weight) x end |
#f(shape:, dfnum:, dfden:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the F-distribution.
155 156 157 158 159 |
# File 'lib/numo/random/generator.rb', line 155 def f(shape:, dfnum:, dfden:, dtype: :float64) x = klass(dtype).new(shape) rng.f(x, dfnum: dfnum, dfden: dfden) x end |
#lognormal(shape:, mean: 0.0, sigma: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to a log-normal distribution.
193 194 195 196 197 |
# File 'lib/numo/random/generator.rb', line 193 def lognormal(shape:, mean: 0.0, sigma: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.lognormal(x, mean: mean, sigma: sigma) x end |
#normal(shape:, loc: 0.0, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to a normal (Gaussian) distribution.
174 175 176 177 178 |
# File 'lib/numo/random/generator.rb', line 174 def normal(shape:, loc: 0.0, scale: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.normal(x, loc: loc, scale: scale) x end |
#random ⇒ Float
Returns random number with uniform distribution in the half-open interval [0, 1).
56 57 58 |
# File 'lib/numo/random/generator.rb', line 56 def random rng.random end |
#seed ⇒ Integer
Returns the seed of random number generator.
36 37 38 |
# File 'lib/numo/random/generator.rb', line 36 def seed rng.seed end |
#seed=(val) ⇒ Object
Sets the seed of random number generator.
43 44 45 |
# File 'lib/numo/random/generator.rb', line 43 def seed=(val) rng.seed = val end |
#standard_t(shape:, df:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Student’s t-distribution.
211 212 213 214 215 |
# File 'lib/numo/random/generator.rb', line 211 def standard_t(shape:, df:, dtype: :float64) x = klass(dtype).new(shape) rng.standard_t(x, df: df) x end |
#uniform(shape:, low: 0.0, high: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of uniformly distributed random values in the interval [low, high).
99 100 101 102 103 |
# File 'lib/numo/random/generator.rb', line 99 def uniform(shape:, low: 0.0, high: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.uniform(x, low: low, high: high) x end |