adding or subtracting depending on the number

2 次查看(过去 30 天)
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.

回答(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 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by