Random

classes
   ACG            discunif       lognormal      poisson        
   MCellRan4      erlang         negexp         repick         
   MLCG           geometric      normal         uniform        
   binomial       hypergeo       play           weibull        
Random

SYNTAX

Random()
Random(seed)
Random(seed, size)

DESCRIPTION

The Random class provides commonly used random distributions which are useful for stochastic simulations. The default distribution is normal with mean = 0 and standard deviation = 1.

This class is an interface to the RNG class from the gnu c++ class library. As of version 5.2, a cryptographic quality RNG class wrapper for mcell_ran4 was added and is available with the MCellRan4 method. The current default random generator is ACG .

Note that multiple instances of the Random class will produce different streams of random numbers only if their seeds are different.

One can switch distributions at any time but if the distribution is stationary then it is more efficient to use r.repick() to avoid constructor/destructor overhead.

EXAMPLES

objref r
r = new Random()
for i=1,10 print r.uniform(30, 50) // not as efficient as
for i=1,10 print r.repick()	   // this
prints 20 random numbers ranging in value between 30 and 50.


ACG

Random

SYNTAX

r.ACG()
r.ACG(seed)
r.ACG(seed, size)

DESCRIPTION

Use a variant of the Linear Congruential Generator (algorithm M) described in Knuth, Art of Computer Programming, Vol. III in combination with a Fibonacci Additive Congruential Generator. This is a "very high quality" random number generator, Default size is 55, giving a size of 1244 bytes to the structure. Minimum size is 7 (total 100 bytes), maximum size is 98 (total 2440 bytes).


MLCG

Random

SYNTAX

r.MLCG()
r.MLCG(seed1)
r.MLCG(seed1, seed2)

DESCRIPTION

Use a Multiplicative Linear Congruential Generator. Not as high quality as the ACG. It uses only 8 bytes.


MCellRan4

Random

SYNTAX

highindex = r.MCellRan4()
highindex = r.MCellRan4(highindex)

DESCRIPTION

Use the MCell variant of the Ran4 generator. See mcell_ran4 . In the no argument case or if the highindex is 0, then the system selects an index which is the random 32 bit integer resulting from an mcell_ran4 call with an index equal to the the number of instances of the Random generator that had been created. Thus, each stream should be statistically independent as long as the highindex values differ by more than the eventual length of the stream. In any case, the initial highindex is returned and can be used to restart an instance of the generator. Use mcell_ran4_init to set the (global) low 32 bit index of the generator.

EXAMPLES

execute following example
objref r, vec, g1, g2, hist
r = new Random()
index = r.MCellRan4()
r.uniform(0, 2)
vec = new Vector(1000)
g1 = new Graph()
g2 = new Graph()
g1.size(0, 1000, 0, 2)
g2.size(0, 2, 0, 150)

proc doit() {
	g1.erase() g2.erase()
	vec.setrand(r)
	hist = vec.histogram(0, 2, 0.2)
	vec.line(g1)
	hist.line(g2, .2)
	g1.flush g2.flush
}
doit()

variable_domain(&index, 0, 2^32-1)
xpanel("MCellRan4 test")
xbutton("Sample", "doit()")
xpvalue("Original index", &index, 1, "r.MCellRan4(index) doit()")
xpanel()


repick

Random

SYNTAX

r.repick()

DESCRIPTION

Pick again from the distribution last used.


play

Random

SYNTAX

r.play(&var)

DESCRIPTION

At the beginning of every call to fadvance and finitialize var is set to a new value equivalent to
var = r.repick()
(but with no interpreter overhead). This is similar in concept to play . Play may be called several times for different variables and each variable will get an independent random value but with the same distribution. To disconnect the Random object from its list of variables, either the variables or the Random object must be destroyed.

EXAMPLES

// run the single script
// use the PointProcessManager to select IClamp
// set dur of IClamp[0] to 100
// open a new Voltage Graph
objref r
r = new Random()
r.poisson(.01)
r.play(&IClamp[0].amp)
//open a RunControl
// press Init&Run several times


uniform

Random

SYNTAX

r.uniform(low, high)

DESCRIPTION

Create a uniform random variable over the open interval low...high].

EXAMPLES

execute following example
objref r, vec, g1, g2, hist
r = new Random()
r.uniform(0, 2)
vec = new Vector(1000)
vec.setrand(r)
hist = vec.histogram(0, 2, 0.2)

g1 = new Graph()
g2 = new Graph()
g1.size(0, 1000, 0, 2)
g2.size(0, 2, 0, 150)
vec.plot(g1)
hist.plot(g2, .2)


discunif

Random

SYNTAX

r.discunif(low, high)

DESCRIPTION

Create a uniform random variable over the discrete integers from low to high.


normal

Random

SYNTAX

r.normal(mean, variance)

DESCRIPTION

Gaussian distribution.

EXAMPLES

execute following example
objref r, g, hist, vec
r = new Random()
r.normal(-1, .5)

vec = new Vector()
vec.indgen(-3, 2, .1)	// x-axis for plot
hist = new Vector(vec.size())
g = new Graph()
g.size(-3, 2, 0, 50)
hist.plot(g, vec)
for(i=0; i<500; i=i+1){
	x = r.repick()
	print i, x
	j = int((x+3)*10) // -3 to 2 -> 0 to 50
	if (j >= 0) {
		hist.x[j] = hist.x[j]+1
	}
	g.flush()
	doNotify()
}


lognormal

Random

SYNTAX

r.lognormal(mean, variance)

DESCRIPTION

Create a logarithmic normal distribution.

EXAMPLES

execute following example
objref r, g, hist, xvec
r = new Random()
r.lognormal(5,2)
n=20
xvec = new Vector(n*3)	// bins look like discrete spikes
for i=0,n-1 {
	xvec.x[3*i] = i-.1
	xvec.x[3*i+1] = i
	xvec.x[3*i+2] = i+.1
}
hist = new Vector(xvec.size())
g = new Graph()
g.size(0, 15, 0, 120)
hist.plot(g, xvec)
for(i=0; i<500; i=i+1){
	x = r.repick()
	print i, x
	j = int(x)
	j = 3*j+1
	if (j >= hist.size()) { // don't let any off the edge
		j = hist.size() -1
	}
	hist.x[j] = hist.x[j]+1
	g.flush()
	doNotify()
}


poisson

Random

SYNTAX

r.poisson(mean)

DESCRIPTION

Create a poisson distribution.

EXAMPLES

execute following example
objref r, g, hist, xvec

r = new Random()
r.poisson(3)

n=20
xvec = new Vector(n*3)
for i=0,n-1 {
	xvec.x[3*i] = i-.1
	xvec.x[3*i+1] = i
	xvec.x[3*i+2] = i+.1
}
hist = new Vector(xvec.size())
g = new Graph()
g.size(0, 15, 0, 120)
hist.plot(g, xvec)
for(i=0; i<500; i=i+1){
	x = r.repick()
	print i, x
	j = int(x)
	j = 3*j+1
	if (j >= hist.size()) {
		j = hist.size() -1
	}
	hist.x[j] = hist.x[j]+1
	g.flush()
	doNotify()
}


binomial

Random

SYNTAX

r.binomial(N,p)

DESCRIPTION

Create a binomial distribution. Returns the number of "successes" after N trials when the probability of a success after one trial is p. (n>0, 0<=p<=1).

P(n, N, p) = p * P(n-1, N-1, p) + (1 - p) * P(n, N-1, p)

EXAMPLES

execute following example
objref r, hist, g
r = new Random()
r.binomial(20, .5)

g = new Graph()
g.size(0, 20, 0, 100)
hist = new Vector(20)
hist.plot(g)
for(i=0; i<500; i=i+1){
	j = r.repick()
	hist.x[j] = hist.x[j]+1
	g.flush()
	doNotify()
}


geometric

Random

SYNTAX

r.geometric(mean)

DESCRIPTION

Create a discrete geometric distribution. Given 0<=mean<=1, return the number of uniform random samples that were drawn before the sample was larger than the mean (always greater than 0.

EXAMPLES

execute following example
objref r, hist, g
r = new Random()
r.geometric(.8)
hist = new Vector(1000)
proc sample() {
	hist = new Vector(1000)
	hist.setrand(r)
	hist = hist.histogram(0,100,1)
	hist.plot(g)
}
g = new Graph()
g.size(0,40,0,200)
sample()
xpanel("Resample")
xbutton("Resample", "sample()")
xpanel()


hypergeo

Random

SYNTAX

r.hypergeo(mean,variance)

DESCRIPTION

Create a hypergeometric distribution.


negexp

Random

SYNTAX

r.negexp(mean)

DESCRIPTION

Create a negative exponential distribution. Distributed as the intervals between events in a poisson distribution.

EXAMPLES

execute following example
objref r, hist, g
r = new Random() 
r.negexp(2.5) 
hist = new Vector(1000)
proc sample() {
        hist = new Vector(1000)
        hist.setrand(r)
        hist = hist.histogram(0,20,.1)
        hist.plot(g, .1)
}
g = new Graph()
g.size(0,20,0,50)
sample()
xpanel("Resample")
xbutton("Resample", "sample()")
xpanel()


erlang

Random

SYNTAX

r.erlang(mean,variance)

DESCRIPTION

Create an Erlang distribution.


weibull

Random

SYNTAX

r.weibull(alpha,beta)

DESCRIPTION

Create a Weibull distribution.


neuron/general/classes/random.hel : 7948 Nov 4