How to generate same set of random numbers?

33 vues (au cours des 30 derniers jours)
Will Smith
Will Smith le 12 Oct 2016
Commenté : Walter Roberson le 13 Juil 2021
How to generate same set of random numbers time and again?

Réponse acceptée

Abhishek Jain
Abhishek Jain le 12 Oct 2016
You can control random number generation using 'rng()' function. The usage is rng(seed).
rng(seed) seeds the random number generator using the nonnegative integer seed so that rand, randi, and randn produce a predictable sequence of numbers.
Hope that helps..!!
  1 commentaire
Will Smith
Will Smith le 12 Oct 2016
Thank you for the quick response.

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 12 Oct 2016
See rng() if you are using a reasonably recent MATLAB.
  1 commentaire
Will Smith
Will Smith le 12 Oct 2016
Thank you for the quick response.

Connectez-vous pour commenter.


Yaxuan Li
Yaxuan Li le 13 Juil 2021
Modifié(e) : Walter Roberson le 13 Juil 2021
Why this doens't work for me?
rng(1,'twister');
x = rand(1,5)
x =
0.4170 0.7203 0.0001 0.3023 0.1468
x = rand(1,5)
x =
0.0923 0.1863 0.3456 0.3968 0.5388
  1 commentaire
Walter Roberson
Walter Roberson le 13 Juil 2021
You need to reset the seed.
rng(1,'twister');
x = rand(1,5)
x = 1×5
0.4170 0.7203 0.0001 0.3023 0.1468
rng(1,'twister');
x = rand(1,5)
x = 1×5
0.4170 0.7203 0.0001 0.3023 0.1468

Connectez-vous pour commenter.

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