From: Robert Feldt Date: 2004-08-15T08:12:11+09:00 Subject: Re: Random variable library? Joel VanderWerf wrote: > Mike wrote: > >> Martin DeMello wrote: >> >> >>>> Anyone know of a Ruby library that defines random variables[1]? >>>> I'd like >>>> access to a variety of distributions. >>> >> >> >>> http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me >> >> >> >> Heh... I was going to say that. :-) >> >> It's just a collection of various distributions that I've seen in >> different places. >> Like Joel said, it starts with Kernel.rand, so it's ultimately no >> better than that. >> But, it should give you different distributions that you need. > > > Not that there's anything wrong with Kernel.rand--it uses MT19937, > which seems to be very good for all but crypto purposes.[1] That plus > Rand.rb would be ideal for a single stream sampled from one of the > many distributions that Rand.rb implements. With caution, you could > use it for a small number of streams, too. > > It's just that if you need arbitrarily many streams of numbers, then > you want to be able to instantiate more generators. Actually, I > suppose you could use Kernel#srand to switch between streams. That > would be slow, because the state vector has to be recreated each time, > but would save on the space needed for all those state vectors. > > [1] http://en.wikipedia.org/wiki/Mersenne_twister > This has been discussed/proposed before so isn't it time someone patched random.c in Ruby sources to work on a MT19937 class instead of the static vars? If matz is pro this change it would be a slim change; introduce typedef struct Rmt { unsigned long state[N]; /* the array for the state vector */ int left = 1; int initf = 0; unsigned long *next; } RMT; and then change the methods to access these instead of the static ones. Come on now, do it and hone your Ruby-c-extension writing skills! The "old" rand + srand would simply work on a global var/constant RANDOM_VALUE which would be an object of the MMT19937 class, ie def rand RANDOM_VALUE.rand end def srand(seed) RANDOM_VALUE.reseed(seed) end etc. Maybe there is already a RCR on this? It is so important in many scientific / simulation apps to be able to have multiple, independent streams of random values. In the mean time one could use http://rubyvm.sourceforge.net/subprojects/randomr/ /R