Generate random sequence number with condition
Show older comments
Hello every one!
I'm a beginner in matlab. Please help me this.
For example, if I want to generate a sequence random numbers (This sequence includes 200numbers) of values from 0.1 to 3.5 provided that the amount of numbers greater than 2 is only 10% of the total. How to do this in matlab. Can someone help me out? Thank you in advance.
4 Comments
Ameer Hamza
on 23 Sep 2020
Exactly 10 percent or approximately 10%?
the cyclist
on 23 Sep 2020
When generating random numbers, it is critically important to fully specify the distribution characteristics. Beginners (at random number generation and at coding) often don't think about this carefully enough.
For example, you cannot have a distribution that obeys both of these restrictions:
- uniformly distributed on the interval [0.1,3.5]
- less than 10% of the values are on the interval [2,3.5]
It may be that a particular sample meets these criteria, but your distribution cannot.
You could create a non-uniform distribution (e.g. an exponentially distributed one) where the distribution range is [0.1,3.5], and on average only 10% of the values are in [2,3.5]. But in a given sample, more than 10% could be in [2,3.5].
Just to try to reinforce these points a bit more. Suppose I roll a 6-sided die. On average, the number 6 will come up less than 20% of the time. But I could get a sample in which it comes up more than 20% of the time. If I try to enforce that the sample has fewer than 20% 6's, then I no longer have a "fair" die.
So, you need to tell us significantly more detail about what you want, and also be clear about your distribution vs a sample from that distribution.
Mi Tung
on 23 Sep 2020
Mi Tung
on 23 Sep 2020
Accepted Answer
More Answers (2)
John D'Errico
on 23 Sep 2020
Edited: John D'Errico
on 23 Sep 2020
I think you still do not appreciate that you cannot just say something is "random" and not define the distribution.
For example, if we generate a sequence of 100 numbers that are EXACTLY either 0.1 OR 3.5, but the probability of 3.5 happens 10% of the time, then it satisfies your requirement. That is trivial to do.
X = (rand(1,100) > 0.9)*3.4 + 0.1;
sum(X>2)/numel(X)
ans =
0.11
So here we have a random sequence where 11% of the 100 numbers exceeded 2. Since the sequence is of length 100, that means we had 11 of the 3.5 events, and 89 of the 0.1 events. As the sequence length goes to infinity, the probability of exceeding 2 will approach 10% quite nicely.
Here are the first 20 events in that sequence:
X(1:20)
ans =
Columns 1 through 9
0.1 0.1 0.1 3.5 0.1 0.1 0.1 3.5 0.1
Columns 10 through 18
0.1 0.1 3.5 0.1 0.1 0.1 0.1 0.1 0.1
Columns 19 through 20
0.1 0.1
It completely satisfies your requirements, but I may postulate, based on your example, that it does not satisfy your goals.
So, do you want some sort of uniform distribution in each sub-interval? That is, it is easy enough to generate a misture distribution, where 10% of the time we have a uniform sample from one interval, and 90% of the time is it uniform over another interval. Trivially easy. Or do you want to see some continuous distribution that has the desired probability. For example, it would be easy enough to derive some sort of truncated exponential, such that by variation of the rate parameter, one could tailor the distribution to what you want. Or we could choose a beta distribution with the same property, and beta distributions have all sorts of shapes.
But until you explain CLEARLY what you are looking for, and what distribution you want, I've given you a perfectly valid solution in one line of code.
Jeff Miller
on 24 Sep 2020
Here is another distributional option with a figure showing the comparison to Bruno's:
%% Bruno's solution
P=interp1([0 0.9 1],[0.1 2 3.5],'pchip','pp');
n = 1e6; % 200 in your case
% here is the array of random
r=ppval(P,rand(1,n));
% this should return value close to 0.1
sum(r>=2)/length(r);
subplot(1,2,1);
histogram(r)
ylabel('Probability density')
xlabel('Random number')
%% Here is another possible distribution with uniform random numbers
% within each interval:
final = zeros(n,1); % the final random numbers will go here
% randomly choose whether each final number is
% in the interval 0.1-2 or the interval 2-3.5
preliminary = rand(n,1);
large = preliminary < 0.10; % only 10% should be large
largeN = sum(large);
final(large) = 2 + rand(largeN,1)*(3.5-2); % random numbers between 2-3.5
final(~large) = 0.1 + rand(n-largeN,1)*(2-0.1); % random numbers between 2-3.5
subplot(1,2,2);
histogram(final,'normalization','pdf')
ylabel('Probability density')
xlabel('Random number')
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!