MATLAB Answers

Generate random number between -Inf and 0.5

5 views (last 30 days)
Z = [-Inf 0.5326 0.6 0.6674 Inf]
I have above intervals like (-Inf 0.5326) (0.5326 0.6) (0.6 0.6674) (0.6674 Inf). Between these intervals, I need to produce random numbers. How do you make the first and last samples? I'm trying to use the formula below, but it's not working. Is there a method to do this, and if so, what is wrong with my formula and how can I fix it? Can anyone please help me with this. Thanks in advance
if i =1
sample(i) = randi([-10^5, za(i+1)],1)
else if i = n
sample(i) = randi([za(i-1), 10^5],1)

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 8 Sep 2021
Since the cummulative-distribution-function of the random-number-distribution has to be one at +infinity, you need to have some probability-density-function that decreases as your Z becomes very small or very large. Therefore you need to add more information to your solution. From the information given above you could simply use something like:
n_samples = 123;
Z = [-Inf 0.5326 0.6 0.6674 Inf];
z = rand(n_samples,1);
for iGroup = numel(Z):-1:2;
z_groups{iGroup-1} = z(Z(iGroup-1)<z&z<=Z(iGroup));
That satisfies your request, in sense that you will get samples in each intervall specified, you havent specified how large fraction of the random numbers are expected to fall in either intervall, nor their probabillity distribution.

More Answers (1)

Walter Roberson
Walter Roberson on 8 Sep 2021
Random numbers with what distribution?
You are looking for infinite tails, but in double precision, representable values get less dense as the magnitude gets larger. The distance between adjacent numbers at 10^20 is 2^14; the distance between adjacent numbers at 10^40 is 2^80. So it is not possible to be able to distinguish between 10^20+0.6 and 10^20+0.6674 (for example) -- not if you use double precision numbers.
So if you want uniform random distribution you are going to have problems, unless you switch to a different representation.
How many different values need to be represented in the range (0.5326 0.6) ? 674 different values, as in 0.5326, 0.5327, 0.5328, ... 0.5998, 0.5999, .6000 ? How many different values betwee -inf and 0.5326 need to be represented ? If you wanted 2^53 different values to be represented between -realmax and 0.5326 and you want uniform random distribution, then the numbers would have to be roughly 10^291 apart. If you went for 2^64 different representable values, they would have to be about 2.4*10^288 apart

Community Treasure Hunt

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

Start Hunting!