Rand with product less than value

4 views (last 30 days)
The number of iterations it takes for the product of two random numbers to produce a number less than 1e-5.
Attempt at solution:
while 2
% randomIntegers < 1e-05
count1 = count1 +1;
randomIntegers = randi([-10,10],[20,1]);
if randomIntergars > 1e-05
break
end
end
EDIT: SECOND ATTEMPT
while z < 1e-05
count1 = count1 + 1;
c = rand(1,1); %rand 1x1 matrix
z = floor(c + (b-c+1) * rand(b,1));
if c > 1e-05
break
end
end
Nothing working no idea why?
  4 Comments
Steven
Steven on 6 Sep 2015
% while z < 1e-05
% count1 = count + 1;
% x = rand(1,1); %rand 1x1 matrix
% z = floor(a + (b-a+1) .* rand(b,1));
% if z < 1e-05
% break
% end
%
% end
I tried this but this doesn't work either
Walter Roberson
Walter Roberson on 22 Apr 2018
Please do not close Questions that have an Answer

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Sep 2015
Edited: Star Strider on 6 Sep 2015
This works when I tried it:
rndprd = 1;
k1 = 1;
while rndprd > 1E-5
a = rand;
b = rand;
rndprd = a*b;
k1 = k1 + 1;
end
fprintf(1,'\n\tRequired %d iterations to produce a product of random numbers (%.6f x %.6f) = %13.5E\n\n',k1, a, b, rndprd)
EDIT — Changed fprintf statement.
  2 Comments
Steven
Steven on 6 Sep 2015
Oh right, I wasn't generating two random numbers that was my problem.
Thanks I think that works

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Sep 2015
Edited: Walter Roberson on 23 Apr 2018
randi() generates random integers. You are generating 20 random integers, not 20 random numbers.
Then you have
if randomIntergars > 1e-05
The randomIntergars > 1e-05 tests whether each of the 20 integers is greater than 1e-05, creating a vector of 20 true and false values. When you apply "if" to a vector of logical values, the result is considered "true" only if all of the entries are true. There are 21 possible integer values in -10 to +10, of which 10 are greater than 1e-5. If the random number generator is "fair", then the probability that all of the entries will be greater than 1e-5 is then (10/21)^20 which is roughly 3.6*10^(-7). It will probably happen eventually, but it might take rather some time -- on average roughly 2.8 * 10^6 trials.
Now, when you do eventually find a list of 20 random numbers from the range with all of them greater than 1e-5, then since the minimum value for each of them will be 1, you can be sure that the minimum value of the product of any 2 or more of them will be 1. But your question is asking about the number of iterations to find a product less than 1e-5.
When you are working with integers, the product of any subset of them is going to be an integer, so the question of whether the product is less than 1e-5 is going to be the same as the question of whether the product is less than or equal to 0. The product of integers is 0 if any of the integers are 0, and otherwise the product is less than 0 if an odd number of the integers is less than 0. For example -9 * +8 = -72 and -72 < 1e-5.
However.. the question asks about the product of "two random numbers". There is nothing there to indicate that the random numbers were to be confined to the integers in -10 to +10, and there is nothing there to indicate that a uniform random distribution is to be used. Perhaps a Normal distribution should be used. Perhaps a Poisson distribution should be used. Perhaps a sum of a negative exponential and a Beta distribution should be used. Your question gives us no way to know.

Community Treasure Hunt

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

Start Hunting!