You are a guest. Restricted access. Read more.
SCaVis manual

This is an old revision of the document!

# Introduction

The SCaVis random numbers can be constructed using several approaches:

• The native Java API for random numbers. See the Java “java.util.Random”
• The native Python API for random numbers
• The native SCaVis classes

# The Native Java approach

Random numbers provided by Java API have already been used in the can be used to generate a single random number. Below we check the methods of this class:

```from java.util import *
r=Random()     # seed from the system time.
r=Random(100L) # user defined seed=100L
dir(r)```

You will see the methods of the Random class:

```  [.. 'nextDouble', 'nextFloat', 'nextGaussian',
...'nextInt', 'nextLong' ..]```

Here is an example of how to fill a list with Gaussian random numbers:

```from java.util import *
r=Random()
g=[]
for i in range(100):
g.append(r.nextGaussian())```

# The Native Python approach

Let us give a simple example which shows how to generate a single random floating point number in the range [0,1] using the Python API:

```>>> from random import *
>>> r=Random()
>>> a=r.randint(1,10) # a random number in range [0.10]```

A random seed from the current system time is used since we do not specify any argument for the Random(). In order to generate a random number predictably for debugging purpose, one should pass an integer (or long) value to the Random(), i.e., for example Random(1L).

One can use the following random number methods:

• r.random() # in range [0.0, 1.0)
• r.randint(min,max) # int in range [min,max]
• r.uniform(min,max) # real number in [min,max]
• r.betavariate(a,b) # Beta distribution (a>0,b>0)
• r.expovariate(lambda) # Exponential distribution
• r.gauss(m,s) # Gaussian distribution with the mean “m” and sigma “s”
• r.lognormvariate(m,s) # Log normal distribution with the mean “m” and sigma “s”
• r.normalvariate(m,s) # Normal distribution with the mean “m” and sigma “s”
• r.gammavariate(a, b) # Gamma distribution.
• r.seed(i) # set seed (i integer or long)
• state=r.getstate() # returns internal state
• setstate(state) # restores internal st

Random numbers are also used for manipulations with lists. One can randomly rearrange elements in a list as:

```>>> list=[1,2,3,4,5,6,7,8,9]
>>> r.shuffle(list)
>>> print list```

The code generated this:

` [3, 4, 2, 7, 6, 5, 9, 8, 1] `

# The Native SCaVis approach

Use the package “cern.jet.random” to build random numbers in SCaVis. Check this out as:

```>>> import cern.jet.random
>>> dir(cern.jet.random)```

This will printout the available classes for generation of random distributions:

```  Beta, Binomial, BreitWigner, BreitWignerMeanSquare,
ChiSquare, Empirical, EmpiricalWalker, Exponential,
ExponentialPower, Gamma, Hyperbolic, HyperGeometric,
Logarithmic, NegativeBinomial, Normal, Poisson,
PoissonSlow, StudentT, Uniform, VonMises, Zeta```

Let us give an example how to generate 100 integer values distributed in accordance with a Poissonian distribution (with the mean 10)

```from cern.jet.random.engine import *
from cern.jet.random  import *
engine=MersenneTwister()
poisson=Poisson(10,engine)
for i in range(100):
print  poisson.nextInt()```

The MersenneTwister is one of the strongest engines. One can use the current system date for a seed to avoid reproducible random numbers:

```>>> from cern.jet.random.engine import *
>>> import java
>>> engine=MersenneTwister(new java.util.Date())```

# Distributions from density functions

Here is a code example showing how to generate random distribution from any given function. Read more deatils in StatisticSample.

```from jhplot  import *
from jhplot.math.StatisticSample  import *
from java.awt import *

c1 = HPlot('Canvas',600,400)
c1.setGTitle('Title')
c1.visible()
c1.setRange(-1,11,0,30)

f=F1D('10+2*cos(x)',0,10)
f.setColor(Color.red)
c1.draw(f)
p=f.getParse()

a=randomRejection(1000,p,15,0,10)
h=H1D('Random numbers',100,0,10)
h.fill(a)
c1.draw(h)```

The output of this program is shown below:

click here if you want to know more

click here if you want to know more

click here if you want to know more

A complete description of how to use Java, Jython and SCaVis for scientific analysis is described in the book Scientific data analysis using Jython and Java published by Springer Verlag, London, 2010 (by S.V.Chekanov)

One can comment and discuss this section after becoming a full member.

Sergei Chekanov 2010/03/07 16:37

man/numeric/random_numbers.1371337590.txt.gz · Last modified: 2013/06/15 17:06 by admin