adding or subtracting depending on the number

조회 수: 2 (최근 30일)
jason
jason 2022년 9월 8일
답변: Walter Roberson 2022년 9월 8일
How do I make it so that if a random number is above a certain amount a number will be subtracted from a diffrent variable. But if the random number is below a certain amount it will be added to a variable.
  댓글 수: 1
David Hill
David Hill 2022년 9월 8일
What have you tried? Can you explain yourself better through a code example?

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

답변 (2개)

Torsten
Torsten 2022년 9월 8일
rng ("default")
amount = 0.5;
variable = 5;
number = 1;
r = rand;
if r >= amount
variable = variable - number;
else
variable = variable + number;
end
variable
variable = 4

Walter Roberson
Walter Roberson 2022년 9월 8일
Variable = Variable + sign(Threshold - RandomNumber) * amount

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by