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

1,717 次查看(过去 30 天)
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 个评论
John BG
John BG 2018-2-18
Hi Kamal
Image Analyst and John D'Errico have supplied the common functions, but perhaps you want to generate a variable length of decimals.
Increasing the length of decimals is really useful to increase randomness, the length may fill up files and files.
Also, you may want to consider surds, and multiples of pi and exp(1), which wouldn't be included with the standard answers so far supplied.
Appreciating time and attention, further comment would be appreciated
John BG
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: 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
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.

请先登录,再进行评论。

更多回答(4 个)

Birdman
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.

请先登录,再进行评论。


Nisha Bharti
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!
  4 个评论

请先登录,再进行评论。


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 个评论
Enis Hidisoglu
Enis Hidisoglu 2021-1-30
You are right. I forget to add right parenthesis. And, I thought he would like to get an integer number between two numbers. But, the code can be changed as "trial = [20:0.001:150]"
Results
Columns 1 through 9
29.3190 28.1360 24.2120 27.7410 24.9270 21.9730 27.0870 20.6080 22.3680
Columns 10 through 18
27.6270 27.0160 22.8730 24.8580 29.9110 20.4510 21.5810 21.5160 28.7570
Columns 19 through 27
25.1480 26.2840 22.5120 21.8350 23.5780 25.6960 26.1980 23.1290 23.8580
Columns 28 through 36
22.8890 29.8310 24.3050 24.2570 28.4380 26.9530 29.0780 27.1720 22.5130
Columns 37 through 45
27.3260 29.4800 25.6630 29.0880 21.5140 24.2630 25.0220 24.7140 24.3610
Columns 46 through 54
28.8860 27.4430 22.7590 23.9140 29.2100 21.4620 28.7280 20.1220 24.5950
Columns 55 through 63
26.4020 28.7360 26.4940 24.0060 22.4790 22.0260 29.0110 23.1800 26.4820
Columns 64 through 72
27.1260 23.0790 23.3760 27.4510 28.2560 25.2530 20.4470 26.2360 25.1870
Columns 73 through 81
25.1790 28.7580 28.1390 28.7960 21.5380 24.9290 29.3090 23.1370 24.3230
Columns 82 through 90
24.8680 27.9270 24.3960 20.8710 26.1820 25.4860 27.2870 27.3760 24.0090
Columns 91 through 99
27.9690 28.3900 21.8400 27.3940 25.2760 27.6700 22.5790 25.9280 28.6650
Columns 100 through 108
24.8250 25.7060 28.2330 29.7700 21.8320 26.8360 27.6150 21.6040 21.1020
Columns 109 through 117
23.5090 25.9910 28.6250 24.1870 29.4670 22.5720 27.4860 20.7710 27.1370
Columns 118 through 126
29.0520 21.6420 27.7430 24.6790 24.3890 21.3720 20.0980 28.8510 24.3870
Columns 127 through 135
25.5050 26.9630 23.9840 24.5200 24.0380 24.4920 20.7450 22.8400 23.1970
Columns 136 through 144
27.9990 26.4930 22.5620 24.8050 25.5470 22.1910 25.5340 26.5250 24.7660
Columns 145 through 153
29.9740 28.0740 23.2480 22.6980 28.4670 28.1700 27.9810 26.4120 29.5750
Columns 154 through 162
20.1730 28.4300 20.3320 26.6490 25.4820 21.4370 27.0990 28.2000 28.7440
Columns 163 through 171
23.8940 22.4050 29.9480 24.3850 21.7910 28.8350 20.6340 26.2450 22.1830
Columns 172 through 180
26.7330 29.5100 28.6370 22.8780 28.2580 28.1980 26.4160 21.0530 26.9990
Columns 181 through 189
28.8260 25.8690 25.6180 21.3460 27.6000 26.3050 28.0280 27.7960 29.7470
Jan
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
Tamas Kis 2021-7-25
Generates random numbers in a range, allows for type specification as well as the desired output size:

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by