adding or subtracting depending on the number
显示 更早的评论
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
2022-9-8
What have you tried? Can you explain yourself better through a code example?
回答(2 个)
rng ("default")
amount = 0.5;
variable = 5;
number = 1;
r = rand;
if r >= amount
variable = variable - number;
else
variable = variable + number;
end
variable
Walter Roberson
2022-9-8
Variable = Variable + sign(Threshold - RandomNumber) * amount
类别
在 帮助中心 和 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!