Skip to content

Commit cfadf50

Browse files
shailesh210translunar
authored andcommitted
Adding seed value, fixes #566 (squashed commit)
1 parent 67115cd commit cfadf50

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/nmatrix/shortcuts.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,12 @@ def block_diagonal(*params)
536536
def random(shape, opts={})
537537
scale = opts.delete(:scale) || 1.0
538538

539-
rng = Random.new
539+
if opts[:seed].nil?
540+
rng = Random.new
541+
else
542+
rng = Random.new(opts[:seed])
543+
end
544+
540545

541546
random_values = []
542547

spec/shortcuts_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@
137137
expect(m.dtype).to eq(:float64)
138138
end
139139

140+
it "creates a matrix of random numbers with defined seed value" do
141+
m1 = NMatrix.random(2,:seed => 62)
142+
m2 = NMatrix.random(2,:seed => 62)
143+
m3 = NMatrix.random(2,:seed => 65)
144+
145+
146+
expect(m1).to eq(m2)
147+
expect(m1).not_to eq(m3)
148+
149+
end
150+
140151
it "creates a complex matrix of random numbers" do
141152
m = NMatrix.random(2, :dtype => :complex128)
142153
end

0 commit comments

Comments
 (0)