r = randi(10,100,1) but using non-uniform distribution
Show older comments
How to create random number from a range non-uniformly distributed?
Thanks!
3 Comments
Jan
on 12 Apr 2013
There is an infinite number of non-uniform distributions. Which one do you want?
Vikas Tiwari
on 4 Nov 2020
how to get random normally distributed numbers in 5by7matrix?
Walter Roberson
on 4 Nov 2020
randn(5,7)
Accepted Answer
More Answers (1)
Steven Lord
on 4 Nov 2020
One way to generate numbers from a "weighted die" is to use discretize with a vector of probabilities. In this example, the weight vector P indicates that I want to generate numbers between 1 and 4 but 1 is twice as likely as each of the other three numbers.
P = [2 1 1 1];
Scale the weight so they sum to 1.
prob = normalize(P, 'scale', sum(P));
Make a vector of probability edges
cprob = cumsum([0 prob]);
Generate some sample data and discretize that sample data to determine into which bin (defined by the probability edges) each of the sample data point falls.
x = rand(1, 1e5);
roll = discretize(x, cprob);
Show a histogram of the rolls and you should see the bar for 1 is twice as high as the bars for 2, 3, and 4.
histogram(roll, 'Normalization', 'probability')
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!