random number generation - set start random number

Anything that doesn't fit elsewhere.
Post Reply
Posts: 14
Joined: Thu Jun 27, 2013 3:46 pm

random number generation - set start random number

Post by srene » Wed Apr 30, 2014 6:54 am


I've got a question setting random numbers.
If I use s.th. like this

Code: Select all

startcell = int(r.uniform(0,100))
This code is inside a for loop so it selects as many random numbers as I want the for loop to do.
Is there a possibility the set the first random number of the random number generation process.


Site Admin
Posts: 5509
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine

Re: random number generation - set start random number

Post by ted » Wed Apr 30, 2014 10:27 am

While you can specify the pseudorandom generator's "seed" (for MCellRan4 or Random123 the "seed" is actually a combination of two or more numbers), you can't specify what number it will return first. But of course there is a workaround that will produce a pseudorandom sequence that starts with a particular number. Instead of using the "raw" values returned by the generator, add a constant so that the sequence begins with the value you want. Here's one way to do this.

Code: Select all

objref r
r = new Random()
NSTART = 3 // or whatever number you want to get on the first pick
kmyrand = 0 // value is changed on the first call to myrand() after initialization
firstcall = 1 // used by myrand to make sure that kmyrand changes on first call after initialization
func myrand() { local x
  x = r.repick()
  if (firstcall==1) {
    firstcall = 0
    kmyrand = NSTART - x
  return x + kmyrand
Notice that this uses the discrete uniform distribution over the interval 0..100. Why bother generating a floating point value if you really only want integers?

func myrand() does all the work. The first time myrand() is called, this function picks a value from the discrete uniform distribution and figures out what constant must be added to it so that you get the "starting value" you want. Subsequent calls to myrand() pick new values from the discunif distribution and add the same constant to them, so the entire pseudorandom sequence will be offset by the same amount.

If you want to run a series of simulations in which each simulation needs a pseudorandom sequence that always starts with the same value but differs after that, use an FInitializeHandler that sets firstcall to 1 at the beginning of each simulation.

Code: Select all

objref fih
fih = new FInitializeHandler("firstcall = 1")

Post Reply