Generating random numbers with a different probabilities

24 ビュー (過去 30 日間)
Omar N
Omar N 2021 年 12 月 1 日
回答済み: Steven Lord 2021 年 12 月 1 日
If I want to generate random numbers between 0 and 150, however i want the probability of having numbers between 0 and 50 to be higher, how can i do it?
  2 件のコメント
John D'Errico
John D'Errico 2021 年 12 月 1 日
I see essentially this same question so often. The problem is, it is not enough to say that you want higher probabilities for some values. That is too vague a question to have an answer, because there are infinitely many distributions that will satisfy your vaguely stated goal. Worse, you have not even stated if you need integer results, or if real numbers are desired.
Omar N
Omar N 2021 年 12 月 1 日
Thank you for your response, I should have illustrated more, but I am new here and I still face difficulties in writing my questions in a proper way.
I want to generate integers between 0 and 150. However, as I’ve mentioned I want to increase and control the probability of having numbers less than 50, If you can recommend one distribution or command that helps me in achieving my goal I would be thankful.

サインインしてコメントする。

回答 (3 件)

Yusuf Suer Erdem
Yusuf Suer Erdem 2021 年 12 月 1 日
Hi there. These codes below are completely original and made by me for you. Each time you need to enter a probability value to the system. When the probability is closer to 1, the system gives more digits from 0-50 range. I hope it works for you. Good luck.
clc; clear; close all;
choice = menu('Choose the case','probability=1','probability=0.7','probability=0.5','probability=0.3');
if choice==1
r = randi([0 50],1,125);
k = randi([50 150],1,25);
elseif choice==2
r = randi([0 50],1,100);
k = randi([50 150],1,50);
else
r = randi([0 50],1,75);
k = randi([50 150],1,75);
end
l=[r k];
  2 件のコメント
Omar N
Omar N 2021 年 12 月 1 日
Thank you this was useful, however is there any specific distribution that i can use to know the probability of having certain number.
In other words, if i want to know the probability of getting 1 or 2 or 150, is there any distribution that I can use?
Yusuf Suer Erdem
Yusuf Suer Erdem 2021 年 12 月 1 日
I never experienced that.

サインインしてコメントする。


Alan Stevens
Alan Stevens 2021 年 12 月 1 日
Assuming there are just two levels of probability, and that the numbers are real, not just integers, you could try:
p50 = 0.75; % probability of number less than 50
N = 10^5; % number of random numbers required
u = rand(N,1); % uniform random numbers
r(u<=p50) = u(u<=p50)*50; % random numbers less than 50
r(u>p50) = u(u>p50)*100 + 50; % random numbers between 50 and 150
  1 件のコメント
Omar N
Omar N 2021 年 12 月 1 日
Thank you this was useful, however is there any specific distribution that i can use to know the probability of having certain number.
In other words, if i want to know the probability of getting 1 or 2 or 150, is there any distribution that I can use?

サインインしてコメントする。


Steven Lord
Steven Lord 2021 年 12 月 1 日
If you know the probabilities you want each number to have you could use discretize. For instance if I want to generate numbers between 1 and 10 with the odd numbers being twice as likely:
P = repmat([2 1], 1, 5)
P = 1×10
2 1 2 1 2 1 2 1 2 1
cumulativeP = [0 cumsum(P)./sum(P)]
cumulativeP = 1×11
0 0.1333 0.2000 0.3333 0.4000 0.5333 0.6000 0.7333 0.8000 0.9333 1.0000
r = rand(1, 1e5); % Random numbers in range (0, 1)
d = discretize(r, cumulativeP); % Bin the random numbers in r using the bins in cumulativeP
h = histogram(d, (1:11)-0.5, 'Normalization', 'probability'); % Show the results
xticks(1:10)
The bars for 1, 3, 5, 7, and 9 are about twice as tall as the bins for 2, 4, 6, 8, and 10 as expected.
shouldBeCloseToP = h.Values./h.Values(end)
shouldBeCloseToP = 1×10
1.9883 0.9840 2.0051 1.0124 1.9748 1.0204 1.9954 0.9886 2.0033 1.0000

製品

Community Treasure Hunt

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

Start Hunting!

Translated by