Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random Number Generator Matlab 6.0

Subject: Random Number Generator Matlab 6.0

From: diegoandresalvarez@lycos.co.uk (Diego Andres Alvarez Marin)

Date: 24 Apr, 2004 12:32:59

Message: 1 of 5

hello:

Can somebody give me some details of Matlab's v6.0 Random Number
Generator? what is the original paper, its period, which statistical
test passes, etc.

Thanks,

Diego Andres

Subject: Random Number Generator Matlab 6.0

From: moler@mathworks.com (Cleve Moler)

Date: 24 Apr, 2004 18:45:00

Message: 2 of 5

In article <55135de4.0404241132.1367d35c@posting.google.com>,
Diego Andres Alvarez Marin <diegoandresalvarez@lycos.co.uk> wrote:
>hello:
>
>Can somebody give me some details of Matlab's v6.0 Random Number
>Generator? what is the original paper, its period, which statistical
>test passes, etc.
>
>Thanks,
>
>Diego Andres

Hi --

See http://www.mathworks.com/moler/random.pdf

  -- Cleve
  moler@mathworks.com

Subject: Random Number Generator Matlab 6.0

From: Sturla Molden

Date: 25 Apr, 2004 01:49:50

Message: 3 of 5

On Sat, 24 Apr 2004 12:32:59 -0700, Diego Andres Alvarez Marin wrote:

> Can somebody give me some details of Matlab's v6.0 Random Number
> Generator? what is the original paper, its period, which statistical
> test passes, etc.

According to Cleve Moler the uniform rng is lagged Fibonacchi generator
with some tweaks and the standard normal generator is Marsaglia's
ziggurat algorithm. You will find details about the generators here:

http://www.mathworks.com/company/newsletters/news_notes/pdf/Cleve.pdf
http://www.mathworks.com/company/newsletters/news_notes/clevescorner/spring01_cleve.html

You will find C code for Marsaglia's ziggurat algorithm in
Marsaglias paper:

http://www.jstatsoft.org/v05/i08/ziggurat.pdf

AFASIK there is no C code for Matlab's rand function available, but
below is my own implementation based on Moler's description and
this additional information:

http://www.mathworks.com/support/solutions/data/8542.html

Beware that my code depends on how the floating point numbera are
represented and therefore not portable between all platforms. But
at least it should work on 32 bit x86 processors under Windows
or Linux.

Sturla Molden


/*
 * Sturla Moldens's attempt to reimplement Matlab's rand function
 *
 * Before calling this function:
 * --- initilise jsr to any unsigned integer
 * --- initialise zbuff[] with calls to (SHR3 * .2328306e-9)
 * --- initialise ulp to eps/2 which is pow(2.0,-53) in Matlab
 *
 * (C) 2003 Sturla Molden
 *
 */


static unsigned long jz, jsr;
#define SHR3 (jz=jsr, jsr^=(jsr<<13), jsr^=(jsr>>17), jsr^=(jsr<<5),jz+jsr)
static float zbuff[32], ulp;

float uniform()
{
      static int i = 0;
      static float b = 0.0;
      union {
          float f;
          unsigned long l;
      } z;
      unsigned long j;
 
      /* subtract with borrow" -- lagged Fibonacchi generator */
      z.f = zbuff[(i+20)%32] - zbuff[(i+5)%32] + b;
      if (z.f < 0.0)
      {
         z.f += 1.0;
         b = ulp;
      }
      else
      b = 0.0;

      /* random bits from SHR3 generator */
      j = SHR3;

      /* zero bits 23-31 (exponent and sign), leave bits 0-22 (floting point fraction) */
      j &= (0xFFFFFFFF >> 9);

      /* bitwise xor of j with z */
      z.l ^= j;

      zbuff[i%32] = z.f;
      i++;
      return z;
}

Subject: Random Number Generator Matlab 6.0

From: moler@acoma.mathworks.com (Cleve Moler)

Date: 25 Apr, 2004 11:09:11

Message: 4 of 5

In article <c6eueu$1s9$1@orkan.itea.ntnu.no>,
Sturla Molden <sturla@molden.net> wrote:

> ...
> AFASIK there is no C code for Matlab's rand function available ...

We now have M-files that exactly mimic the built-in random number
generators. See

   http://www.mathworks.com/moler/ncm/randtx.m
   http://www.mathworks.com/moler/ncm/randntx.m

as well as

   http://www.mathworks.com/moler/random.pdf

  -- Cleve
  moler@mathworks.com

Subject: Random Number Generator Matlab 6.0

From: sturla@molden.net

Date: 25 Apr, 2004 21:21:54

Message: 5 of 5

In comp.soft-sys.matlab Cleve Moler <moler@acoma.mathworks.com> wrote:


> We now have M-files that exactly mimic the built-in random number
> generators. See

> http://www.mathworks.com/moler/ncm/randtx.m
> http://www.mathworks.com/moler/ncm/randntx.m

This is a nice way of documenting the random number generators.


S.M.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us