create Non-uniform distribution

13 views (last 30 days)
nirit
nirit on 26 Dec 2015
Answered: Roger Stafford on 27 Dec 2015
Hello!
I want to create non -uniform distribution using rand. for example, let X be random integer number with :
X=
0 w.p 1/3
1 w.p 1/2
2 w.p 1/6
meaning, with p =1/3 x is equal to 0, with p=1/2 x is equal to 1, and with p=1/6 x is equal to 2.
how can I set a series of Xi random numbers using rand (1,N)?
Thanks In Advance.

Accepted Answer

Roger Stafford
Roger Stafford on 27 Dec 2015
This should do it:
x = rand;
x = (x>1/3)+(x>5/6);

More Answers (1)

Image Analyst
Image Analyst on 26 Dec 2015
Perhaps you mean randi() or randperm() instead of rand(). Anyway, here's one way of many:
N = 1000
% Assign numbers in the specified amounts.
% Assign the 0's.
X = zeros(1,N);
% Assign the 1's.
i1 = round(N/3+1)
i2 = i1 + round(N/2)
X(i1:i2) = 1;
% Assign the 2's.
X(i2+1:end) = 2;
% Now scramble them to give them a random order.
X = X(randperm(length(X)));
% Check distribution to verify.
counts = histogram(X, 'BinMethod', 'Integers', 'Normalization', 'pdf');

Community Treasure Hunt

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

Start Hunting!