ruby-numo / numo-narray

Ruby/Numo::NArray - New NArray class library
http://ruby-numo.github.io/narray/
BSD 3-Clause "New" or "Revised" License
415 stars 41 forks source link

how we set the seed of random so to get deterministic results? #82

Closed jackxxu closed 6 years ago

jackxxu commented 6 years ago

for example:

require 'numo/narray'

Numo::DFloat.new(1).rand #=> [0.794815]
Numo::DFloat.new(1).rand #=> [0.201042]

I wonder if there can be something like one of the following

  1. numpy's
    np.random.seed(random_seed)
  2. ruby stdlib random's
    seeded = Random.new(42) # create a new instance of Random seeded with 42
    p seeded.rand(5..10) 
masa16 commented 6 years ago

use Numo::NArray.srand(random_seed)

$ irb -r numo/narray
irb(main):001:0> Numo::NArray.srand(123)
=> nil
irb(main):002:0> Numo::DFloat.new(1).rand 
=> Numo::DFloat#shape=[1]
[0.473321]
irb(main):003:0> Numo::NArray.srand(123)
=> nil
irb(main):004:0> Numo::DFloat.new(1).rand 
=> Numo::DFloat#shape=[1]
[0.473321]
jackxxu commented 6 years ago

now I see it at https://github.com/ruby-numo/numo-narray/blob/0ddd65e377cdcf01159e2a990fb2b1bd3bf51f77/ext/numo/narray/rand.c#L70.

thanks!