Rand with product less than value
이전 댓글 표시
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
Walter Roberson
2015년 9월 6일
Please describe the result that you expected
randi([-10,10],[20,1])
to have ?
Steven
2015년 9월 6일
Steven
2015년 9월 6일
Walter Roberson
2018년 4월 22일
Please do not close Questions that have an Answer
채택된 답변
추가 답변 (1개)
Walter Roberson
2015년 9월 6일
편집: Walter Roberson
2018년 4월 23일
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.
카테고리
도움말 센터 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!