[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Multiple random number generators

Frantisek Fuka

4/29/2006 2:04:00 PM

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.

2 Answers

Robert Klemme

4/30/2006 9:47:00 AM

0

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?sea...
http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_...

Kind regards

robert

Robert Klemme

4/30/2006 11:13:00 AM

0

Robert Klemme <bob.news@gmx.net> 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?sea...
> http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_...
>
> 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