How to generate random number vector that satisfies the following equation
2 views (last 30 days)
Show older comments
How to generate random number vector that satisfies the following equation
0.005<= Current Vector + rand() <= 0.03.
Note: random numbers can be positive or negative as along as they satisfy the equation.
Thanks in advance
Venki
1 Comment
Walter Roberson
on 18 Apr 2013
What is the maximum magnitude you wish to use for the random generation?
What is "Current Vector" for this purposes?
What does it mean for a vector to be within a particular range? Do you mean each component of the vector, or do you mean norm() of the vector ?
Answers (1)
Image Analyst
on 18 Apr 2013
Try this:
CurrentVector = 0.001; % whatever you want.
count = 1;
while count < 100000 % Failsafe - bail out after this many with no success.
randomValue = rand;
value = CurrentVector + randomValue;
if 0.005 <= value && value <= 0.03
fprintf('Criteria satisfied with a random value of %f\n', randomValue);
break; % out of the while loop
else
fprintf('Criteria NOT satisfied with a random value of %f\n', randomValue);
end
end
You'll see something like this:
Criteria NOT satisfied with a random value of 0.075854
Criteria NOT satisfied with a random value of 0.053950
Criteria NOT satisfied with a random value of 0.530798
Criteria NOT satisfied with a random value of 0.779167
Criteria NOT satisfied with a random value of 0.934011
Criteria NOT satisfied with a random value of 0.129906
Criteria NOT satisfied with a random value of 0.568824
Criteria NOT satisfied with a random value of 0.469391
Criteria satisfied with a random value of 0.011902
0 Comments
See Also
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!