How do I generate a random number between two numbers,?

3.081 views (last 30 days)
how i get random number between two numbers , like i want random number between 20-150 like this , random, i know the max number and the minimum number and i want to matlab gave me a random number between the max and the minimum
Walter Roberson
Walter Roberson on 18 Feb 2018
John BG:
rand() effectively generates an integer in the range [0, 2^53-1], retries if the result was 0, and then divides the integer now in the range [1, 2^53-1] by 2^53 to give the random value. It is not possible to get higher precision than that over any range that starts above 1 .
There are two higher precision random generators available for values less than 1: mlfg6331_64 can generate some multiples of 2^(-64) that the usual generator cannot.
swb2712 can generate all possible representable numbers in the range (0,1) including with range down below 1E-308. However, swb2712 does not generate equally spaced numbers, and the mean for it (half the numbers are less) is 1.11875110968003101149364479731944007560647073019006103414072774998094175776051774574042623356006417050422674048837541521801190921428379539907292472443881270372218911884781720662428061113996528944121698315475958721481213258908827606800475302345048341134463455488980253230058090875047749429690054193911503277704529859897485122299798376843682490289211273193359375e-154
As long as you stick to double precision, you cannot have uniform distribution over an arbitrary range (a,b) with more than 1 part in 2^53 precision. For example although (1, 3) has more than 2^53 representable numbers, the ones between 2 and 3 are not located the same distance apart as the ones between 1 and 2.
Any use of surds tends to bias the distribution. sqrt(rand) has an over 70% chance of being below 0.5 . Multiplying by pi or exp(1) does not increase precision.
If you need more than 53 bits of precision then you should probably be switching to Symbolic Toolbox or to the multi-precision toolbox in the File Exchange.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 18 Feb 2018
The second example for rand() says this:
"In general, you can generate N random numbers in the interval (a,b) with the formula r = a + (b-a).*rand(N,1)."
With a=20 and b=150, you get what you want.
r = 20 + (150-20) .* rand(N,1)
  1 Comment
John D'Errico
John D'Errico on 18 Feb 2018
Both valid answers of course, depending on whether you want random integers or uniformly continuous samples.

Sign in to comment.

More Answers (4)

Birdman on 18 Feb 2018
randi([20 150])
  1 Comment
John D'Errico
John D'Errico on 18 Feb 2018
Both valid answers of course, depending on whether you want random integers or uniformly continuous samples.

Sign in to comment.

Nisha Bharti
Nisha Bharti on 12 Jan 2022
To generate a random number between [a,b]:
r = a + (b-a)*rand();
To generate N numbers:
r = a + (b-a)*rand(1,N);
Hope that helps!
Jan on 23 Oct 2022
Yes, it does work. And the formula has been provided 4 years before already by Image Analyst.

Sign in to comment.

Enis Hidisoglu
Enis Hidisoglu on 29 Jan 2021
Hello my friend,
you can use below codes to generate random number between two number which you want; for example between 20-150,
trial = [20:1:150];
r = randperm(lenght(trial));
trial = trial(r)
I think this will be useful for you...
Best wishes
Jan on 30 Jan 2021
Edited: Jan on 23 Oct 2022
@Enis Hidisoglu: It is not a good idea to create a huge list of value by trial = 20:0.001:150 only to get some elements from it. See Image Analyst's answer:
r = 20 + (150-20) .* rand(N, 1)
This produces the random values directly with a higher precision.

Sign in to comment.

Tamas Kis
Tamas Kis on 25 Jul 2021
Generates random numbers in a range, allows for type specification as well as the desired output size:


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!

Translated by