필터 지우기
필터 지우기

While problem, generating random numbers

조회 수: 1 (최근 30일)
Marta Gonzalez
Marta Gonzalez 2020년 9월 7일
댓글: Marta Gonzalez 2020년 9월 10일
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
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?)
Marta Gonzalez
Marta Gonzalez 2020년 9월 10일
Ohh now I undertsand what you mean, I was wrong in my perspective of the problem. Now it really works.
Thanks both of you

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by