While problem, generating random numbers
조회 수: 1 (최근 30일)
이전 댓글 표시
Write a function called one_so_small that takes no input arguments
and returns three scalars as output arguments. If it is called this way,
[a,b,c] = one_so_small, then it repeatedly gets three numbers from the
function rand until the numbers pass this test: ten times one of the numbers
is smaller than the product of the other two numbers. For example, if the
three random numbers are 0.4, 0.03, and 0.9, then ten times the second number
is 0.3 and the product of the first and third numbers is 0.36, so ten times
one of these three numbers is smaller than the product of the other two numbers.
On the other hand, if the second number were 0.038, these three numbers
would not pass the test. Here are two sample calls. Before the sample
calls are made, the random number generator is initialized by means of rng.
In this case, it is initialized with the number 12 (non-negative integer). The
purpose of this use of rng is to make it possible to compare your results with
those below.
>> rng(12)
>> [a,b,c] = one_so_small
a =
0.014575
b =
0.91875
c =
0.53374
>> [a,b,c] = one_so_small
a =
0.033421
b =
0.95695
c =
0.90071
댓글 수: 8
Geoff Hayes
2020년 9월 9일
Marta - you are assigning random values to a, b, and c, but like Mohammad says you need to assign the values you a,b,c before you check the condition.
while true
a=rand;
b=rand;
c=rand;
if % your conditions - what would they be?
% exit the loop
end
end
So you keep generating your 3 random numbers until one of the three conditions is satisifed. And once satisfied, you can exit the loop? (How would you do this?)
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!