Skip to content

Commit

Permalink
feat: support mersenne twister
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshoku committed Nov 20, 2022
1 parent 48dbde5 commit bb511cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/numo/random/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ class Generator # rubocop:disable Metrics/ClassLength
# Creates a new random number generator.
#
# @param seed [Integer] random seed used to initialize the random number generator.
# @param algorithm [String] random number generation algorithm ('pcg32', 'pcg64').
def initialize(seed: nil, algorithm: 'pcg64')
# @param algorithm [String] random number generation algorithm ('mt32', 'mt64', 'pcg32', and 'pcg64').
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'
Expand Down
28 changes: 23 additions & 5 deletions spec/numo/random/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
let(:algorithm) { 'pcg64' }

describe '#initialize' do
context "when algorithm args is 'pcg64'" do
let(:algorithm) { 'pcg64' }
context "when algorithm args is 'mt32'" do
let(:algorithm) { 'mt32' }

it 'uses PCG32 class for random number generator', :aggregate_failures do
expect(rng.algorithm).to eq('pcg64')
expect(rng.instance_variable_get(:@rng)).to be_a(Numo::Random::PCG64)
it 'uses MT32 class for random number generator', :aggregate_failures do
expect(rng.algorithm).to eq('mt32')
expect(rng.instance_variable_get(:@rng)).to be_a(Numo::Random::MT32)
end
end

context "when algorithm args is 'mt64'" do
let(:algorithm) { 'mt64' }

it 'uses MT64 class for random number generator', :aggregate_failures do
expect(rng.algorithm).to eq('mt64')
expect(rng.instance_variable_get(:@rng)).to be_a(Numo::Random::MT64)
end
end

Expand All @@ -24,6 +33,15 @@
end
end

context "when algorithm args is 'pcg64'" do
let(:algorithm) { 'pcg64' }

it 'uses PCG64 class for random number generator', :aggregate_failures do
expect(rng.algorithm).to eq('pcg64')
expect(rng.instance_variable_get(:@rng)).to be_a(Numo::Random::PCG64)
end
end

context 'when wrong algorithm args given' do
let(:algorithm) { 'none' }

Expand Down

0 comments on commit bb511cb

Please sign in to comment.