random number generating RANDI with percentage

조회 수: 19 (최근 30일)
Takim Mustakim
Takim Mustakim 2022년 11월 7일
답변: James Tursa 2022년 11월 7일
How to generating random number with percentage:
value = 0 - 14 with 86%
value = 15 - 28 with 14%
with 1000 array
  댓글 수: 1
Torsten
Torsten 2022년 11월 7일
Is 86% and 14% also a probability (i.e. random numbers between 0 and 14 are generated with probability 0.86 and random numbers between 15 and 28 are generated with probability 0.14) or a fixed percentage ?

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

답변 (3개)

Image Analyst
Image Analyst 2022년 11월 7일
If it's not homework, you can try this:
x = [0, 14, 28];
y = [0, .86, 1.0];
plot(x, y, 'b.-', markerSize=30, LineWidth=2)
grid on;
title("Cumulative Distribution Function")
% Get half a million random numbers
n = 500000
n = 500000
r = GetRandom(n);
% Count # less than 14. Should be 86% of them.
pct14 = sum(r < 14) / n
pct14 = 0.8596
% Count # more than 14. Should be 14% of them.
pct14 = sum(r > 14) / n
pct14 = 0.1404
% Function to give 86% of numbers in the range of 0-14 and 14% of the
% numbers in the range 14-28.
function r = GetRandom(n)
r = rand(1, n);
if r < 0.86
r = r / 0.86;
else
slope = .14 / 14;
% y - y0 = slope * (x - x0)
% Find x
% x = (y - y0) / slope + x0
% y0 = 0.86. x0 = 14.
r = (r - 0.86) / slope + 14;
end
end
Uses the concept of inverse transform sampling.
If it's your homework you can't turn in my code.
  댓글 수: 3
Image Analyst
Image Analyst 2022년 11월 7일
@Takim Mustakim it's right there in the code I posted. Did you not click the "Copy" button in the gray-shaded code block? Did you just click and drag over part of the code? THe code was a bit split up because I ran it in the text box so it interspersed results with the code. That's why you should have clicked the "Copy" button. Anyway, here is the code all in one chunk.
x = [0, 14, 28];
y = [0, .86, 1.0];
plot(x, y, 'b.-', markerSize=30, LineWidth=2)
grid on;
title("Cumulative Distribution Function")
% Get half a million random numbers
n = 500000
r = GetRandom(n);
% Count # less than 14. Should be 86% of them.
pct14 = sum(r < 14) / n
% Count # more than 14. Should be 14% of them.
pct14 = sum(r > 14) / n
% Function to give 86% of numbers in the range of 0-14 and 14% of the
% numbers in the range 14-28.
function r = GetRandom(n)
r = rand(1, n);
if r < 0.86
r = r / 0.86;
else
slope = .14 / 14;
% y - y0 = slope * (x - x0)
% Find x
% x = (y - y0) / slope + x0
% y0 = 0.86. x0 = 14.
r = (r - 0.86) / slope + 14;
end
end
Torsten
Torsten 2022년 11월 7일
I think you overlooked the "randi" in the title.

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


Walter Roberson
Walter Roberson 2022년 11월 7일
If you have the Statistics Toolbox, use randsample()
If not then:
Find a number, S, such that S*p is an integer for each probability in the vector p. If you have probabilities such as sqrt(2)/2 that are mathematically irrational then you will need to find a good approximation of an integer rather than being exact.
Once you have S, take each element Value(K) and repeat it round(S*p(K)) times, and collect the repeated values in a vector. For example for .86 and .14 you could use S = 50 and repeat the first value 43 times and the second value 7 times.
Now to draw with weights, use randi() on the length of the vector with repetitions, to pick out elements of the vector. The probability that any one (repeated) element will be drawn will be proportional to the number of repetitions divided by the total length of the vector. For example, 43 copies of 0 out of 50 elements gives a 43/50 = 0.86 probability that 0 would be drawn.

James Tursa
James Tursa 2022년 11월 7일
One way:
n = 1000;
p = 0.86;
r = rand(n,1);
x = r < p;
m = sum(x);
r(x) = randi([0,14],m,1);
r(~x) = randi([15,28],n-m,1);
Or one could re-use the random numbers already in r, but this seems a bit complicated:
n = 1000;
p = 0.86;
r = rand(n,1);
x = r < p;
r(x) = floor(15*r(x));
r(~x) = floor(14*(r(~x)-p)/(1-p)) + 15;

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by