Effacer les filtres
Effacer les filtres

Obtaining same values at avery simulation using rand function

1 vue (au cours des 30 derniers jours)
Salvatore Mazzarino
Salvatore Mazzarino le 14 Sep 2012
I'm building some simulations with matlab and I use rand function. I would obtain at every run, the same results. I read somewhere I have to set the seed of rand function. I tried using
s = RandStream('mcg16807', 'seed', 0)
RandStream.setGlobalStream(s);
but it didn't work.Maybe I made some mistake.

Réponse acceptée

Wayne King
Wayne King le 14 Sep 2012
I think you should use the newer rng
rng default;
x = randn(100,1);
rng default
y = randn(100,1);
max(abs(x-y))
x and y are the same sequence.
In general, you can capture the random generator settings and resuse them
S = rng;
x = randn(100,1);
rng(S)
y = randn(100,1);

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 14 Sep 2012
y=rand(1,100);
save file y
%when you need y
load file

Oleg Komarov
Oleg Komarov le 14 Sep 2012
Modifié(e) : Oleg Komarov le 14 Sep 2012
You can use rng()
rng(1)
rand(1,5)
rng(1)
rand(1,5)
Or with your approach:
s = RandStream('mcg16807', 'seed', 0);
RandStream.setGlobalStream(s);
rand(1,5)
s = RandStream('mcg16807', 'seed', 0);
RandStream.setGlobalStream(s);
rand(1,5)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by