Multiple random number generators

Discussion in 'Ruby' started by Frantisek Fuka, Apr 29, 2006.

  1. It seems to me that rand and srand can only use the single "global"
    psudorandom generator. Is there a possibility to have several random
    sequences based on different seeds, all available at the same time?

    I could probably write this myself but I don't want to reinvent the
    wheel.

    I don't need the generator to be "cryptographically secure" or have
    extremely long periods (I'll rarely use random sequences longer than
    100 numbers from any single generator) but I need to have several
    generators available at the same time and possibility to "reset" their
    sequences individually.
     
    Frantisek Fuka, Apr 29, 2006
    #1
    1. Advertising

  2. Frantisek Fuka wrote:
    > It seems to me that rand and srand can only use the single "global"
    > psudorandom generator. Is there a possibility to have several random
    > sequences based on different seeds, all available at the same time?
    >
    > I could probably write this myself but I don't want to reinvent the
    > wheel.
    >
    > I don't need the generator to be "cryptographically secure" or have
    > extremely long periods (I'll rarely use random sequences longer than
    > 100 numbers from any single generator) but I need to have several
    > generators available at the same time and possibility to "reset" their
    > sequences individually.
    >

    You'll find quite a lot of stuff on RAA

    http://raa.ruby-lang.org/search.rhtml?search=random
    http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_minor=Math

    Kind regards

    robert
     
    Robert Klemme, Apr 30, 2006
    #2
    1. Advertising

  3. Robert Klemme <> wrote:
    > Frantisek Fuka wrote:
    >> It seems to me that rand and srand can only use the single "global"
    >> psudorandom generator. Is there a possibility to have several random
    >> sequences based on different seeds, all available at the same time?
    >>
    >> I could probably write this myself but I don't want to reinvent the
    >> wheel.
    >>
    >> I don't need the generator to be "cryptographically secure" or have
    >> extremely long periods (I'll rarely use random sequences longer than
    >> 100 numbers from any single generator) but I need to have several
    >> generators available at the same time and possibility to "reset"
    >> their sequences individually.
    >>

    > You'll find quite a lot of stuff on RAA
    >
    > http://raa.ruby-lang.org/search.rhtml?search=random
    > http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_minor=Math
    >
    > Kind regards
    >
    > robert


    PS: if it's for testing purposes only and you have small sequences you could
    pre generate them (or store the sequence while you generate it) and this
    allow for easy reset.

    class MyRandom
    def initialize(size = 0)
    @rands = []
    @index = 0
    size.times { rand }
    reset
    end

    def rand(limit = 0)
    x = get_next
    limit == 0 ? x : (limit * x).to_i
    end

    def reset() @index = 0 end

    def clear()
    @rands = []
    reset
    end

    private

    def get_next
    res = @rands[@index] ||= Kernel.rand
    @index += 1
    res
    end
    end


    Kind regards

    robert
     
    Robert Klemme, Apr 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RadiationX

    Random Number Generators....

    RadiationX, Feb 26, 2006, in forum: C Programming
    Replies:
    40
    Views:
    1,030
    stathis gotsis
    Mar 3, 2006
  2. cesco

    random number generators

    cesco, Feb 3, 2006, in forum: C++
    Replies:
    4
    Views:
    380
    cesco
    Feb 3, 2006
  3. Daniel Dyer
    Replies:
    15
    Views:
    551
    Hendrik Maryns
    Nov 26, 2007
  4. orz
    Replies:
    39
    Views:
    1,046
    James Kanze
    Jun 23, 2010
  5. Bart Braem

    Separate random number generators?

    Bart Braem, Jan 22, 2009, in forum: Ruby
    Replies:
    18
    Views:
    194
    Bart Braem
    Jan 27, 2009
Loading...

Share This Page