43

How can I generate a random number in MATLAB between 13 and 20?

Amro
  • 123,847
  • 25
  • 243
  • 454
crowso
  • 2,049
  • 9
  • 33
  • 38
  • possible duplicate of [MATLAB generate random numbers](http://stackoverflow.com/questions/1892375/matlab-generate-random-numbers) – Bobby Feb 22 '11 at 12:03
  • 2
    possible duplicate of [Is there a way in Matlab using the pseudo number generator to generate numbers within a specific range?](http://stackoverflow.com/questions/2152334/is-there-a-way-in-matlab-using-the-pseudo-number-generator-to-generate-numbers-wi) – gnovice Feb 23 '11 at 04: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 Jul 02 '19 at 19:28

9 Answers9

60

If you are looking for Uniformly distributed pseudorandom integers use:

randi([13, 20])
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
zellus
  • 9,617
  • 5
  • 39
  • 56
  • cant n = 13 + (rand(1) * 7) give Uniformly distributed pseudorandom integers ? – crowso Feb 22 '11 at 12:41
  • 2
    @user581544: not unless you call round(n). – zellus Feb 22 '11 at 12:46
  • 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)`. – Evgeni Sergeev Sep 19 '13 at 06: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 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. – James Jan 31 '19 at 01: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. – SecretAgentMan Jan 31 '19 at 04:26
9

http://www.mathworks.com/help/techdoc/ref/rand.html

n = 13 + (rand(1) * 7);
TimCodes.NET
  • 4,601
  • 1
  • 25
  • 36
6
r = 13 + 7.*rand(100,1);

Where 100,1 is the size of the desidered vector

pcofre
  • 3,976
  • 18
  • 27
1

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
ash_bobham
  • 11
  • 1
0

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

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
0

You can also use:

round(mod(rand.*max,max-1))+min
keyser
  • 18,829
  • 16
  • 59
  • 101
moksef
  • 344
  • 1
  • 5
  • 17
  • 1
    Is not in range [min,max] and also not uniformly distributed, which is implicitly implied by the question. – knedlsepp Feb 05 '15 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` – patrik Apr 10 '15 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. – moksef Apr 14 '15 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? – moksef Apr 14 '15 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. – patrik Apr 14 '15 at 20:21
  • Further, I have got the feeling that modulus starts repeating itself after some time. I am not sure though. That is just a feeling. However introducing a random element within the modulus should help prevent this if properly designed I guess. Modulus can be used for the purpose of pseudo random number generation, but the design needs to be tested and the coefficients that can be used for good randomness is just a small set. Selecting one of these, then scaling would be better. Further I would say that `rand` does a pretty good job already :). – patrik Apr 14 '15 at 20:28
  • @moksef: Your function will yield values from `min` to `max+min-1` and the probability of `max+min-1` and `min+1` is off. – knedlsepp Apr 14 '15 at 22:29
0

Generate values from the uniform distribution on the interval [a, b].

      r = a + (b-a).*rand(100,1);
user570593
  • 3,420
  • 12
  • 56
  • 91
0

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

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
0

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

PyMatFlow
  • 459
  • 4
  • 8