Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom seed for pseudo-random number generator

Tags:

I need to perform few tests where I use randn pseudo random number generator. How can I set the seed on my own, so every time I run this test I will get the same results? (yeah, I know it's a little bit weird, but that's the problem).

I've found the RANDSTREAM object that has the seed property, but it's read only. Is there any way to use it for seeding the generator?

like image 985
Gacek Avatar asked Jan 13 '11 15:01

Gacek


People also ask

What is the seed of a pseudorandom number generator?

A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator. For a seed to be used in a pseudorandom number generator, it does not need to be random.

How do you seed random numbers?

For example, “take a number x, add 900 +x, then subtract 52.” In order for the process to start, you have to specify a starting number, x (the seed). Let's take the starting number 77: Add 900 + 77 = 977. Subtract 52 = 925.

How will you set the starting value in generating random numbers?

The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time. Use the seed() method to customize the start number of the random number generator.

Why is seed 42?

What is the significance of random. seed(42) ? It's a pop-culture reference! In Douglas Adams's popular 1979 science-fiction novel The Hitchhiker's Guide to the Galaxy, towards the end of the book, the supercomputer Deep Thought reveals that the answer to the great question of “life, the universe and everything” is 42.


1 Answers

The old way of doing it:

randn('seed',0) 

The new way:

s = RandStream('mcg16807','Seed',0) RandStream.setDefaultStream(s) 

Note that if you use the new way, rand and randn share the same stream so if you are calling both, you may find different numbers being generated compared to the old method (which has separate generators). The old method is still supported for this reason (and legacy code).

See http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html for more info.

like image 190
James Avatar answered Oct 15 '22 04:10

James