3.0

3.0 | 2 ratings Rate this file 2 Downloads (last 30 days) File Size: 1.39 KB File ID: #12747

Random number generation

by Paul de Wit

 

22 Oct 2006 (Updated 25 Oct 2006)

Function which generation n random numbers between 0 and 1

| Watch this File

File Information
Description

Function which generation n random numbers between 0 and 1 using the formula ri = (177786 ri-1 + 64654 ri-6) mod (2^31-1)

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
23 Oct 2006 John D'Errico

If you care even remotely about speed of execution, this is not the code you wish to use.

>> tic,r = prng(10000,6);toc
Elapsed time is 2.749655 seconds.
>> tic,r = rand(1,10000);toc
Elapsed time is 0.001816 seconds.

Who cares if the period of this RNG is as large as the author claims? As slow as this code is, you will never come close to that period.

I'd suggest using Matlab's builtin rand. If you prefer the Twister algorithm, it too is available now in matlab. Older Matlab releases can use Peter's code from the file exchange.

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=6614&objectType=file

23 Oct 2006 Paul de Wit

Attempted to decrease the time of execution

tic,r = prng(10000,6);toc
Elapsed time is 0.033910 seconds.
tic,r = rand(1,10000);toc
Elapsed time is 0.000387 seconds.

Unfortuately still no where near as good as Matlab please suggest any further improvements

24 Oct 2006 John D'Errico

This seems quite a bit better for speed now. Its still not as fast as the builtin rand, but I imagine to get maximum speed from this you would need carefully written compiled code. Thanks for the improvement.

I've noticed one other point since - if you call this repeatedly with the same starting integer, you get the same exact pseudo-random sequence.

If you call it repeatedly with a different starting point, then the sequence is the same for the for the first few values. Changing the starting point introduces an offset.

>> r = prng(10,7)'
r =
        0.763
      0.20333
      0.40782
      0.48588
     0.076487
      0.13995
       0.4621
      0.27007
      0.67838
       0.2846

>> r = prng(10,8)'
r =
      0.20333
      0.40782
      0.48588
     0.076487
      0.13995
      0.86102
        0.669
     0.077306
      0.68352
      0.91051

So, if you need many random numbers, generate the entire set of numbers that you will ever need in one call.

To repair this, can this RNG be set to pick up where the last call left off? Could you use a persistent variable to have it remember the final state of the RNG?

16 Nov 2006 Paul Connelly

How can I backtrack a PRN. I'm trying to find an algorithm that I can scramble and then unscramble with PRN. Any ideas pointers?

Please login to add a comment or rating.
Updates
24 Oct 2006

Reduce the speed of execution significantly, if anyone can increase the speed further I would be happy to update the file thanks John D'Errico

25 Oct 2006

Added persistant seed values

tic,prng(10000);,toc
Elapsed time is 0.003760 seconds.

Tag Activity for this File
Tag Applied By Date/Time
approximation Paul de Wit 22 Oct 2008 08:45:07
interpolation Paul de Wit 22 Oct 2008 08:45:07
random Paul de Wit 22 Oct 2008 08:45:07
generation Paul de Wit 22 Oct 2008 08:45:08
l ecuyer et al Paul de Wit 22 Oct 2008 08:45:08
mathematics Paul de Wit 22 Oct 2008 08:45:08

Contact us at files@mathworks.com