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

조회 수: 4,195(최근 30일)
kamal faris 2018년 2월 18일
댓글: Jan 2022년 10월 23일
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
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Walter Roberson 2018년 2월 18일
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: https://www.mathworks.com/help/matlab/math/creating-and-controlling-a-random-number-stream.html#brvku_2 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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Image Analyst 2018년 2월 18일
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표시숨기기 없음
John D'Errico 2018년 2월 18일
Both valid answers of course, depending on whether you want random integers or uniformly continuous samples.

댓글을 달려면 로그인하십시오.

### 추가 답변(4개)

Birdman 2018년 2월 18일
randi([20 150])
##### 댓글 수: 1표시숨기기 없음
John D'Errico 2018년 2월 18일
Both valid answers of course, depending on whether you want random integers or uniformly continuous samples.

댓글을 달려면 로그인하십시오.

Nisha Bharti 2022년 1월 12일
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!
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Jan 2022년 10월 23일
Yes, it does work. And the formula has been provided 4 years before already by Image Analyst.

댓글을 달려면 로그인하십시오.

Enis Hidisoglu 2021년 1월 29일
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
##### 댓글 수: 5표시숨기기 이전 댓글 수: 4
Jan 2021년 1월 30일
편집: Jan 2022년 10월 23일
@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.

댓글을 달려면 로그인하십시오.

Tamas Kis 2021년 7월 25일
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!