Class: Numo::Random::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/numo/random/generator.rb

Overview

Generator is a class that generates random number with several distributions.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 496)
x = rng.uniform(shape: [2, 5], low: -1, high: 2)

p x
# Numo::DFloat#shape=[2,5]
# [[1.90546, -0.543299, 0.673332, 0.759583, -0.40945],
#  [0.334635, -0.0558342, 1.28115, 1.93644, -0.0689543]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed: nil, algorithm: 'pcg64') ⇒ Generator

Creates a new random number generator.

Parameters:

  • seed (Integer) (defaults to: nil) —

    random seed used to initialize the random number generator.

  • algorithm (String) (defaults to: 'pcg64') —

    random number generation algorithm ('mt32', 'mt64', 'pcg32', and 'pcg64').



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/numo/random/generator.rb', line 28

def initialize(seed: nil, algorithm: 'pcg64') # rubocop:disable Metrics/MethodLength
  @algorithm = algorithm.to_s
  @rng = case @algorithm
         when 'mt32'
           MT32.new(seed: seed)
         when 'mt64'
           MT64.new(seed: seed)
         when 'pcg32'
           PCG32.new(seed: seed)
         when 'pcg64'
           PCG64.new(seed: seed)
         else
           raise ArgumentError, "Numo::Random::Generator does not support '#{@algorithm}' algorithm"
         end
end

Instance Attribute Details

#algorithm ⇒ String

Returns random number generation algorithm.

Returns:

  • (String)


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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 42)
x = rng.bernoulli(shape: 1000, p: 0.4)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • p (Float) —

    probability of success.

  • dtype (Symbol) (defaults to: :int32) —

    data type of random array.

Returns:

  • (Numo::IntX | Numo::UIntX)


83
84
85
# File 'lib/numo/random/generator.rb', line 83

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 42)
x = rng.binomial(shape: 1000, n: 10, p: 0.4)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • n (Integer) —

    number of trials.

  • p (Float) —

    probability of success.

  • dtype (Symbol) (defaults to: :int32) —

    data type of random array.

Returns:

  • (Numo::IntX | Numo::UIntX)


100
101
102
103
104
# File 'lib/numo/random/generator.rb', line 100

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.cauchy(shape: 100, loc: 0.0, scale: 1.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • loc (Float) (defaults to: 0.0) —

    location parameter.

  • scale (Float) (defaults to: 1.0) —

    scale parameter.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


294
295
296
297
298
# File 'lib/numo/random/generator.rb', line 294

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.chisquare(shape: 100, df: 2.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • df (Float) —

    degrees of freedom, must be > 0.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


312
313
314
315
316
# File 'lib/numo/random/generator.rb', line 312

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).

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 42)
w = Numo::DFloat[0.1, 0.6, 0.2]
x = rng.discrete(shape: [3, 10], weight: w)

p x

# Numo::Int32#shape=[3,10]
# [[1, 1, 1, 1, 1, 1, 1, 1, 2, 1],
#  [0, 1, 0, 1, 1, 0, 1, 1, 2, 1],
#  [2, 1, 1, 1, 1, 2, 2, 1, 1, 2]]

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • weight (Numo::DFloat | Numo::SFloat) —

    (shape: [n]) list of probabilities of each integer being generated.

  • dtype (Symbol) (defaults to: :int32) —

    data type of random array.

Returns:

  • (Numo::IntX | Numo::UIntX)


256
257
258
259
260
# File 'lib/numo/random/generator.rb', line 256

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.exponential(shape: 100, scale: 2)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • scale (Float) (defaults to: 1.0) —

    scale parameter, lambda = 1.fdiv(scale).

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


155
156
157
158
159
# File 'lib/numo/random/generator.rb', line 155

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.f(shape: 100, dfnum: 2.0, dfden: 4.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • dfnum (Float) —

    degrees of freedom in numerator, must be > 0.

  • dfden (Float) —

    degrees of freedom in denominator, must be > 0.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


331
332
333
334
335
# File 'lib/numo/random/generator.rb', line 331

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.gamma(shape: 100, k: 9, scale: 0.5)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • k (Float) —

    shape parameter.

  • scale (Float) (defaults to: 1.0) —

    scale parameter.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


174
175
176
177
178
# File 'lib/numo/random/generator.rb', line 174

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 42)
x = rng.geometric(shape: 1000, p: 0.4)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • p (Float) —

    probability of success on each trial.

  • dtype (Symbol) (defaults to: :int32) —

    data type of random array.

Returns:

  • (Numo::IntX | Numo::UIntX)


137
138
139
140
141
# File 'lib/numo/random/generator.rb', line 137

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.gumbel(shape: 100, loc: 0.0, scale: 1.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • loc (Float) (defaults to: 0.0) —

    location parameter.

  • scale (Float) (defaults to: 1.0) —

    scale parameter.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


193
194
195
196
197
# File 'lib/numo/random/generator.rb', line 193

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.lognormal(shape: 100, mean: 0.0, sigma: 1.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • mean (Float) (defaults to: 0.0) —

    mean of normal distribution.

  • sigma (Float) (defaults to: 1.0) —

    standard deviation of normal distribution.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


369
370
371
372
373
# File 'lib/numo/random/generator.rb', line 369

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 42)
x = rng.negative_binomial(shape: 1000, n: 10, p: 0.4)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • n (Integer) —

    number of trials.

  • p (Float) —

    probability of success.

  • dtype (Symbol) (defaults to: :int32) —

    data type of random array.

Returns:

  • (Numo::IntX | Numo::UIntX)


119
120
121
122
123
# File 'lib/numo/random/generator.rb', line 119

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.normal(shape: 100, loc: 0.0, scale: 1.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • loc (Float) (defaults to: 0.0) —

    location parameter.

  • scale (Float) (defaults to: 1.0) —

    scale parameter.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


350
351
352
353
354
# File 'lib/numo/random/generator.rb', line 350

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new(seed: 42)
x = rng.poisson(shape: 1000, mean: 4)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • mean (Float) (defaults to: 1.0) —

    mean of poisson distribution.

  • dtype (Symbol) (defaults to: :int32) —

    data type of random array.

Returns:

  • (Numo::IntX | Numo::UIntX)


211
212
213
214
215
# File 'lib/numo/random/generator.rb', line 211

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).

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
v = rng.random

Returns:

  • (Float)


67
68
69
# File 'lib/numo/random/generator.rb', line 67

def random
  rng.random
end

#seed ⇒ Integer

Returns the seed of random number generator.

Returns:

  • (Integer)


47
48
49
# File 'lib/numo/random/generator.rb', line 47

def seed
  rng.seed
end

#seed=(val) ⇒ Object

Sets the seed of random number generator.

Parameters:

  • val (Integer) —

    random seed.



54
55
56
# File 'lib/numo/random/generator.rb', line 54

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.standard_t(shape: 100, df: 8.0)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • df (Float) —

    degrees of freedom, must be > 0.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


387
388
389
390
391
# File 'lib/numo/random/generator.rb', line 387

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).

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.uniform(shape: 100, low: -1.5, high: 1.5)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • low (Float) (defaults to: 0.0) —

    lower boundary.

  • high (Float) (defaults to: 1.0) —

    upper boundary.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


275
276
277
278
279
# File 'lib/numo/random/generator.rb', line 275

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.

Examples:

require 'numo/random'

rng = Numo::Random::Generator.new
x = rng.weibull(shape: 100, k: 5, scale: 2)

Parameters:

  • shape (Integer | Array<Integer>) —

    size of random array.

  • k (Float) —

    shape parameter.

  • scale (Float) (defaults to: 1.0) —

    scale parameter.

  • dtype (Symbol) (defaults to: :float64) —

    data type of random array.

Returns:

  • (Numo::DFloat | Numo::SFloat)


230
231
232
233
234
# File 'lib/numo/random/generator.rb', line 230

def weibull(shape:, k:, scale: 1.0, dtype: :float64)
  x = klass(dtype).new(shape)
  rng.weibull(x, k: k, scale: scale)
  x
end