how to generate uniformly random varaibles with inequality constraints
Show older comments
Hi,
I have two variables function f(c1,c2) and I would like to randomly generates c1 and c2 with the constraints that
- Maximum >= f(c1,c2)>=C here C is a constant, which is less than the optima for f in a constraint area
- c1>=0, c2>=0,
- c1+c2<=100
Do you know you to realize this? For example, as in the image below, I would like take a random point of (c1,c2) in the darkest blue area.

Accepted Answer
More Answers (1)
the cyclist
on 1 Aug 2013
Edited: the cyclist
on 1 Aug 2013
What you describe is the "rejection method", and it does not violate the randomness that you want.
Here is an example that is similar to what you want:
% The function that defines one of the criteria
f = @(x,y) x.^2 + y.^2;
% Number of points to generate
nPoints = 1000;
% Pre-allocate memory for the random data
c1 = zeros(nPoints,1);
c2 = zeros(nPoints,1);
% Initialize counter for the number of points we have
i = 0;
% Run a loop until we have the number we need
while i < nPoints
% Generate trial points
c1_try = randi([0 100]);
c2_try = randi([0 100]);
% If these points meet the criteria, store them and increment the counter
if (c1_try+c2_try < 100) && f(c1_try,c2_try) > 3000
i = i+1;
c1(i) = c1_try;
c2(i) = c2_try;
end
end
% Plot the resulting points
figure
plot(c1,c2,'.')
This is not the most efficient method. (For example, you could speed this up by generating all the random numbers upfront.) But I thought it might be better to keep it simple so that you can see what is going on.
Categories
Find more on Creating and Concatenating Matrices 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!