Period Length of the random numbers generated by rand() and randn()

12 vues (au cours des 30 derniers jours)
Sofie Brammer
Sofie Brammer le 8 Mai 2023
Commenté : Sofie Brammer le 8 Mai 2023
Hello,
from what I understand, (uniform) random number generators will produce numbers that at some point will repeat.
Is this always the case? Is it possible to read somewhere how long this period length is for the uniform number generator rand()?
And does randn() use rand()? Is there a way to see how randn() is coded?
What I actually would like to know is the Period Length of randn(), if the output of this is actually repeating after a while.
Thank you very much for your help! :)
Greetings
Sofie

Réponse acceptée

Steven Lord
Steven Lord le 8 Mai 2023
See the "Choosing a Random Number Generator" section on this documentation page for a brief description of each of the available random number generators in MATLAB and their approximate periods.
We do not distribute the source code for either rand or randn.
  1 commentaire
Sofie Brammer
Sofie Brammer le 8 Mai 2023
Perfect, that is what I was searching for.
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (1)

Jonas
Jonas le 8 Mai 2023
Modifié(e) : Jonas le 8 Mai 2023
don't confuse yourself, random number do not have a period length, but what you actually mean may be the rate e.g. after which a number may appear again. this is not deterministic, but you can measure e.g. the mean number of lements after which a specific number appears again:
short example with numbers 1 to 100
numberOfPossibilities=100;
data=randi(numberOfPossibilities,1e6,1);
for nr=1:numberOfPossibilities
where=find(data==nr);
spacings=diff(where);
meanSpacing(nr,1)=mean(spacings);
end
disp(meanSpacing')
Columns 1 through 19 99.8758 100.6833 101.6778 101.5189 99.8014 100.6575 100.1841 101.7151 100.2505 100.5372 100.0663 98.9614 101.1416 98.2634 97.5752 101.8207 99.1696 99.2484 99.4164 Columns 20 through 38 100.1717 100.1275 100.4499 99.7139 98.7456 101.6561 98.9993 99.8174 99.9619 100.3358 98.5253 97.4752 99.9273 99.0303 101.2657 100.9726 101.1186 97.5004 101.2726 Columns 39 through 57 100.8305 99.5587 98.4710 99.4178 99.3804 98.8412 99.9486 100.9847 99.5266 100.0800 101.2774 100.0746 99.4222 100.0274 98.1896 98.3103 100.2012 99.7047 99.8475 Columns 58 through 76 99.8735 100.9039 101.2885 101.5181 99.3311 101.0973 100.2707 99.2883 100.9896 98.5731 101.1047 101.6660 100.6660 100.0169 99.9413 101.3303 101.2446 100.1078 99.8385 Columns 77 through 95 100.6228 100.3901 99.5440 100.2714 99.1937 99.6922 99.7770 100.2451 99.4812 99.2587 100.0448 100.6201 98.9741 99.1781 99.5432 99.6016 100.5860 100.1544 98.4221 Columns 96 through 100 100.5930 101.2390 98.7681 101.2734 99.6102
as you can see, this number is near the number of possible integer values
but the output almost never repeats itself if there are enough ouput values possible. E.g. if you have 1000 possible outputs and a sequence of 1e6 values, the probability of an exact repetition is (1/1000)^(1e6)
note again: random numbers are random, if you have uniform distribution, the probability of a specific number is always the the same, regardless if you have drawn a number for the first time or for the 1000th time.
  1 commentaire
Sofie Brammer
Sofie Brammer le 8 Mai 2023
Modifié(e) : Sofie Brammer le 8 Mai 2023
Thank you very much for your answer! :)
I was already worrying that the term "period length" might be confusing, I just could not find a better phrase.
What I mean by that is not that a single random number would have a period length, but that if i generate a veeeery long sequence (or vector) of random numbers (e.g. using rand()), then this would (for my understanding) at some point repeat.
Because what we generate are not really random umbers, but pseudo-random numbers according to some algorithm, e.g. Mersenne-Twister MT19937ar (https://en.wikipedia.org/wiki/Mersenne_Twister). And these algorithms are periodic.
In the "Handbook of Monte Carlo Methods" (Kroese) I read that this MT19937ar algorithm was (at 2011) the default for random number generation in MATLAB.
But I am neither sure that it is explicitely (and still) behind rand() nor that randn() uses rand() - so I am not sure if I can conclude that the period length is that of MT19937ar.
Still, I guess I don't need to worry that the period length might be too short using MATLAB build-in functions :D
Would just be nice to know for sure about what is going on.

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