Multiple random number generators

F

Frantisek Fuka

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.
 
R

Robert Klemme

Frantisek said:
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
 
R

Robert Klemme

Robert Klemme said:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top