Period Length of the random numbers generated by rand() and randn()
11 views (last 30 days)
Show older comments
Sofie Brammer
on 8 May 2023
Commented: Sofie Brammer
on 8 May 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
0 Comments
Accepted Answer
Steven Lord
on 8 May 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.
More Answers (1)
Jonas
on 8 May 2023
Edited: Jonas
on 8 May 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')
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.
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!