How to control the generation of random variables

1 Ansicht (letzte 30 Tage)
Tania
Tania am 13 Mai 2015
Kommentiert: Tania am 13 Mai 2015
Hi there,
for R2014a rng(1) can be used to control the generation ff random number for rand function.
However; for R2010a I am not sure how to control the generation of numbers for rand function like rng(1) does.
The reason is I do not want to generate random numbers every time i run the program and get different results.
c = 5
rng(1)
a = rand(2,c)
it gives same numbers every time in R2014 however; I need to use this in R2010 as I do not have R2014 and I can not use rng.
Any suggestion will be appreciated.

Akzeptierte Antwort

Guillaume
Guillaume am 13 Mai 2015
The simplest way might be to use the deprecated (in 2010 and up) 'seed' option of rand:
rand('seed', 1);
a = rand(2, 5)
'seed' has been deprecated because it doesn't just change the seed but also the type of number generator. The proper way to just change the seed would be to replace the default number generator by a copy with a new seed. You'd use RandStream. See the 2010 documentation of rand for a detailed example:
RandStream.setDefaultStream(RandStream('mt19937ar','seed',1));
a = rand(2, 5);
  1 Kommentar
Tania
Tania am 13 Mai 2015
Thank you very much. My problem solved using
rand('seed',1);
a = rand(2,5);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by