How can I generate a random number in MATLAB between 13 and 20?
-
possible duplicate of MATLAB generate random numbersBobby– Bobby2011-02-22 12:03:20 +00:00Commented Feb 22, 2011 at 12:03
-
2possible duplicate of Is there a way in Matlab using the pseudo number generator to generate numbers within a specific range?gnovice– gnovice2011-02-23 04:05:06 +00:00Commented Feb 23, 2011 at 4:05
-
This requires more info....do you want them to be continuous between [13, 20]? Is the interval [13,20] or (13,20), etc. Should they be equally likely? Do you have a distribution in mind?SecretAgentMan– SecretAgentMan2019-07-02 19:28:35 +00:00Commented Jul 2, 2019 at 19:28
Add a comment
|
9 Answers
If you are looking for Uniformly distributed pseudorandom integers use:
randi([13, 20])
5 Comments
crowso
cant n = 13 + (rand(1) * 7) give Uniformly distributed pseudorandom integers ?
zellus
@user581544: not unless you call round(n).
Evgeni Sergeev
@crowso Not at all actually. The values 13 and 20 will get half the probability of the others. Use this to see what I mean:
hist(round(13 + (rand(1, 10000) * 7)), 8).James
@SecretAgentMan It is a uniform distribution, but it's on the open interval (0,1), so 0 and 1 will never come up. Type
help rand in MATLAB ... "returns an N-by-N matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval(0,1). " The bigger reason why 13 and 20 come up half as often is because round rounds to the nearest integer. Since the function for n will give results >13 and <20, 13 and 20 are missing more than half of their opportunities. Note: I'm basing this info off of MATLAB R2017b.SecretAgentMan
@James, I must have been thinking of
randn. You are right: rand is indeed generating from U(0,1). The issue of open or closed interval is not practically significant as the probability of generating any number on [0,1] is zero (to include endpoints). The probability density function is defined on the closed interval [0,1]. Not sure what lack of sleep amounted to my previous comment's error. Thank you for the correction.You can also use:
round(mod(rand.*max,max-1))+min
7 Comments
knedlsepp
Is not in range [min,max] and also not uniformly distributed, which is implicitly implied by the question.
patrik
Modulus is in general hard to predict and hard to use for random number generation. In general it is easy to end up in a non-random pattern. Better to just use
randmoksef
@knedlsepp: In order to generality purpose, I mention min for 13, and max for 20 (it is implicitly implied! maybe it is better to edit the question to min and max). could you please indicate what is the probability distribution of the result.
moksef
@patrik: I didn't review the implementation of that function. Would you please explain why we miss the random pattern when we use modulus? Do you perform any experiment?
patrik
@moksef I did actually. I guess that it rather is the unbiased pattern that disappear. In most cases modulus likes to form up around some specific numbers. These are in general dependent on which numbers are selected. Try to generate a set of numbers using your function. Then plot the histogram. Try to repeat that for different numbers. Especially, try to set
max-1 to be a binary number and also a prime number. I have not tried that for this algorithm, but the guess is that these should perform better. |
Best solution is randint , but this function produce integer numbers.
You can use rand with rounding function
r = round(a + (b-a).*rand(m,n));
This produces Real random number between a and b , size of output matrix is m*n
2 Comments
Sardar Usama
randint is obsoleted. In older versions, it requires Communications Toolbox. It is not a good idea to use this function anymore even if you're using an old versionSecretAgentMan
randi generates uniformly distributed integers on a set range