How can I generate a random number in MATLAB between 13 and 20?
-
possible duplicate of MATLAB generate random numbers– BobbyFeb 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?– gnoviceFeb 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?– SecretAgentManJul 2, 2019 at 19:28
9 Answers
If you are looking for Uniformly distributed pseudorandom integers use:
randi([13, 20])
-
cant n = 13 + (rand(1) * 7) give Uniformly distributed pseudorandom integers ?– crowsoFeb 22, 2011 at 12:41
-
2
-
3@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)
. Sep 19, 2013 at 6:22 -
@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 becauseround
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.– JamesJan 31, 2019 at 1:58 -
@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. Jan 31, 2019 at 4:26
ocw.mit.edu is a great resource that has helped me a bunch. randi is the best option, but if your into number fun try using the floor function with rand to get what you want.
I drew a number line and came up with
floor(rand*8) + 13
You can also use:
round(mod(rand.*max,max-1))+min
-
1Is not in range [min,max] and also not uniformly distributed, which is implicitly implied by the question. Feb 5, 2015 at 15:55
-
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
rand
– patrikApr 10, 2015 at 14:12 -
@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.– moksefApr 14, 2015 at 17:23
-
@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?– moksefApr 14, 2015 at 17:28
-
@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.– patrikApr 14, 2015 at 20:21
Generate values from the uniform distribution on the interval [a, b].
r = a + (b-a).*rand(100,1);
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
-
1
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 version Jun 25, 2018 at 10:35 -
randi
generates uniformly distributed integers on a set range Dec 18, 2018 at 19:38
if you are looking to generate all the number within a specific rang randomly then you can try
r = randi([a b],1,d)
a
= start point
b
= end point
d
= how many number you want to generate but keep in mind that d should be less than or equal to b-a
If you need a floating random number between 13 and 20
(20-13).*rand(1) + 13
If you need an integer random number between 13 and 20
floor((21-13).*rand(1) + 13)
Note: Fix problem mentioned in comment "This excludes 20" by replacing 20 with 21