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
-
#bernoulli(shape:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to the Bernoulli distribution.
-
#binomial(shape:, n:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to a binomial distribution.
-
#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).
-
#exponential(shape:, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values with an exponential distribution.
-
#f(shape:, dfnum:, dfden:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the F-distribution.
-
#gamma(shape:, k:, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values with a gamma distribution.
-
#geometric(shape:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to a geometric distribution.
-
#gumbel(shape:, loc: 0.0, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Gumbel 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.
-
#negative_binomial(shape:, n:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to a negative binomial 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.
-
#poisson(shape:, mean: 1.0, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to the Poisson 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).
-
#weibull(shape:, k:, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values with the Weibull distribution.
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
#bernoulli(shape:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to the Bernoulli distribution.
72 73 74 |
# File 'lib/numo/random/generator.rb', line 72 def bernoulli(shape:, p:, dtype: :int32) binomial(shape: shape, n: 1, p: p, dtype: dtype) end |
#binomial(shape:, n:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to a binomial distribution.
89 90 91 92 93 |
# File 'lib/numo/random/generator.rb', line 89 def binomial(shape:, n:, p:, dtype: :int32) x = klass(dtype).new(shape) rng.binomial(x, n: n, p: p) x end |
#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.
283 284 285 286 287 |
# File 'lib/numo/random/generator.rb', line 283 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.
301 302 303 304 305 |
# File 'lib/numo/random/generator.rb', line 301 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).
245 246 247 248 249 |
# File 'lib/numo/random/generator.rb', line 245 def discrete(shape:, weight:, dtype: :int32) x = klass(dtype).new(shape) rng.discrete(x, weight: weight) x end |
#exponential(shape:, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values with an exponential distribution.
144 145 146 147 148 |
# File 'lib/numo/random/generator.rb', line 144 def exponential(shape:, scale: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.exponential(x, scale: scale) x end |
#f(shape:, dfnum:, dfden:, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the F-distribution.
320 321 322 323 324 |
# File 'lib/numo/random/generator.rb', line 320 def f(shape:, dfnum:, dfden:, dtype: :float64) x = klass(dtype).new(shape) rng.f(x, dfnum: dfnum, dfden: dfden) x end |
#gamma(shape:, k:, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values with a gamma distribution.
163 164 165 166 167 |
# File 'lib/numo/random/generator.rb', line 163 def gamma(shape:, k:, scale: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.gamma(x, k: k, scale: scale) x end |
#geometric(shape:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to a geometric distribution.
126 127 128 129 130 |
# File 'lib/numo/random/generator.rb', line 126 def geometric(shape:, p:, dtype: :int32) x = klass(dtype).new(shape) rng.geometric(x, p: p) x end |
#gumbel(shape:, loc: 0.0, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values according to the Gumbel distribution.
182 183 184 185 186 |
# File 'lib/numo/random/generator.rb', line 182 def gumbel(shape:, loc: 0.0, scale: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.gumbel(x, loc: loc, scale: scale) 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.
358 359 360 361 362 |
# File 'lib/numo/random/generator.rb', line 358 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 |
#negative_binomial(shape:, n:, p:, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to a negative binomial distribution.
108 109 110 111 112 |
# File 'lib/numo/random/generator.rb', line 108 def negative_binomial(shape:, n:, p:, dtype: :int32) x = klass(dtype).new(shape) rng.negative_binomial(x, n: n, p: p) 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.
339 340 341 342 343 |
# File 'lib/numo/random/generator.rb', line 339 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 |
#poisson(shape:, mean: 1.0, dtype: :int32) ⇒ Numo::IntX | Numo::UIntX
Generates array consists of random values according to the Poisson distribution.
200 201 202 203 204 |
# File 'lib/numo/random/generator.rb', line 200 def poisson(shape:, mean: 1.0, dtype: :int32) x = klass(dtype).new(shape) rng.poisson(x, mean: mean) 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.
376 377 378 379 380 |
# File 'lib/numo/random/generator.rb', line 376 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).
264 265 266 267 268 |
# File 'lib/numo/random/generator.rb', line 264 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 |
#weibull(shape:, k:, scale: 1.0, dtype: :float64) ⇒ Numo::DFloat | Numo::SFloat
Generates array consists of random values with the Weibull distribution.
219 220 221 222 223 |
# File 'lib/numo/random/generator.rb', line 219 def weibull(shape:, k:, scale: 1.0, dtype: :float64) x = klass(dtype).new(shape) rng.weibull(x, k: k, scale: scale) x end |