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


Random numbers

Snippet from Wikipedia: Random number generation

A random number generator (RNG) is a computational or physical device designed to generate a sequence of numbers or symbols that lack any pattern, i.e.


The SCaVis random numbers can be constructed using several approaches:

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:

Third-party libraries

ScaVis contains a number of third-party Java libraries that can be used for computation of density (PDF), cumulative (CDF), quantile, and random variates of many popular statistical distributions, such as:

  • Ansari-Bradley
  • Beta
  • Binomial
  • Cauchy
  • Chi square
  • Exponential
  • Fisher's F
  • Gamma
  • Geometric
  • Hypergeometric
  • Kendall
  • Logistic
  • Log normal
  • Negative binomial
  • Non-central beta,
  • Non-central chi square,
  • Non-central F
  • Non-central T
  • Normal
  • Poisson
  • Sign rank
  • Spearman
  • Student's T
  • Tukey
  • Uniform
  • Weibull
  • Wilcoxon

and many others. Here is the list you can access and include into your programs: Look at the following Java packages:

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)

Sergei Chekanov 2010/03/07 16:37

man/numeric/random_numbers.txt · Last modified: 2014/12/13 13:17 by admin
CC Attribution-Share Alike 3.0 Unported
Powered by PHP Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 Valid HTML5