MATLAB Answers

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

3,998 ビュー (過去 30 日間)
kamal faris
kamal faris 2018 年 2 月 18 日
回答済み: Tamas Kis 2021 年 7 月 25 日
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 件のコメント
Walter Roberson
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: 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
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
John D'Errico 2018 年 2 月 18 日
Both valid answers of course, depending on whether you want random integers or uniformly continuous samples.


その他の回答 (3 件)

Birdman 2018 年 2 月 18 日
randi([20 150])
  1 件のコメント
John D'Errico
John D'Errico 2018 年 2 月 18 日
Both valid answers of course, depending on whether you want random integers or uniformly continuous samples.


Enis Hidisoglu
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 件のコメント
Jan 2021 年 1 月 30 日
@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
Tamas Kis 2021 年 7 月 25 日
Generates random numbers in a range, allows for type specification as well as the desired output size:


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by