• Edit
• Info
• Attachments

# Scilab's rand

This page is devoted to Scilab's rand function. Users who had the curiosity to dig in the source code of Scilab, may find that the source which is hiding behind the Scilab's rand function is based on a congruential generator, called "URAND". The goal of this page is to analyse the origin of this source code and to provide some tests about the quality of this generator.

As an introduction, it must be said that this is not the best generator available in Scilab. More efficient sequences are available with the grand primitive, for example the Mersenne-Twister algorithm or the KISS generator.

## Origin of the source code

These are the comments of the source code :

```c      urand is a uniform random number generator based  on  theory  and
c  suggestions  given  in  d.e. knuth (1969),  vol  2.   the integer  iy
c  should be initialized to an arbitrary integer prior to the first call
c  to urand.  the calling program should  not  alter  the  value  of  iy
c  between  subsequent calls to urand.  values of urand will be returned
c  in the interval (0,1).```

Everybody who has a little interest for random number generation has read Knuth's "Art of Computer Programming" [1], volume 2 "Seminumerical Algorithms". In fact, the source code was not written by Knuth's himself, but was published in January 1973 by Michael A. Malcolm and Cleve Moler, in a technical report of the Computer Science Department of Stanford University [2]. It is interesting to know that M.A. Malcom has also written another paper "Algorithms to reveal properties of floating-point arithmetic", [3]. Some algorithms which are explained in that paper are available in Scilab, with an interface to the DLAMCH routine of the LAPACK project. The %eps variable is computed with that.

The URAND routine is based on the following iterative procedure :

`x = (IA * x + IC) mod M`

where the constants are

```IA = 843314861
IC = 453816693
M = 2^31```

In fact, the algorithm itself does not compute the value of M directly, but rather computes it with an algorithm. "URAND discovers the value of m/2 by testing successive powers of 2 until a multiplication by 2 produces no increase in magnitude."

To do...

## References

[1] The Art of Computer Programming, D. Knuth, http://en.wikipedia.org/wiki/The_Art_of_Computer_Programming

[2] URAND, A UNIVERSAL RANDOM NUMBER GENERATOR BY, MICHAEL A. MALCOLM, CLEVE B. MOLER, STAN-CS-73-334, JANUARY 1973, COMPUTER SCIENCE DEPARTMENT, School of Humanities and Sciences, STANFORD UNIVERSITY, ftp://reports.stanford.edu/pub/cstr/reports/cs/tr/73/334/CS-TR-73-334.pdf

[3] Michael A. Malcolm, Algorithms to reveal properties of floating-point arithmetic, Communications of the ACM archive, Volume 15 , Issue 11 (November 1972)

public: Scilab's rand (last edited 2011-12-08 11:23:05 by favignana)