You are a guest. Restricted access. Read more.

This is an old revision of the document!


Random numbers


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

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 *
for i in range(100):

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 *
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)
h=H1D('Random numbers',100,0,10)

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