64 views (last 30 days)

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.

Bruno Luong
on 23 Sep 2020 at 18:49

Edited: Bruno Luong
on 23 Sep 2020 at 18:49

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)

the cyclist
on 23 Sep 2020 at 19:52

John D'Errico
on 23 Sep 2020 at 17:08

Edited: John D'Errico
on 23 Sep 2020 at 17:13

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 at 0:04

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')

Opportunities for recent engineering grads.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021291

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021291

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021327

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021327

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021330

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021330

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021357

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/598585-generate-random-sequence-number-with-condition#comment_1021357

Sign in to comment.