Picking random variables with different probablities

I have a variable range lets say x = 3:6.
I want to pick 200 random numbers from this range (it could be any decimal number in the range). But I want these numbers to be picked with certain probability.
For example 20% of the numbers from the range 3:4,
50% of the numbers from the range 4:5,
and rest 30% of the numbers from the range 5:6.
How can i do this?
Thanks.

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 22 日
編集済み: Ameer Hamza 2020 年 3 月 22 日

1 投票

Try this
a = randsample(3:5, 10000, true, [0.2 0.5 0.3]) + rand(1, 10000);
It will generate 10000 random numbers with given distribution. Check their distribution with
histogram(a, [3 4 5 6])

2 件のコメント

bhuvan khoshoo
bhuvan khoshoo 2020 年 3 月 22 日
Thank you!!!
This is perfect. It did not click me that I can use rand(1) to add with numbers generated by randsample.
Thank you so much!!
Ameer Hamza
Ameer Hamza 2020 年 3 月 22 日
Glad to be of Help.

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

その他の回答 (0 件)

製品

リリース

R2019a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by