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 Kommentar

What have you tried? Can you explain yourself better through a code example?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Torsten
Torsten am 8 Sep. 2022
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 am 8 Sep. 2022

0 Stimmen

Variable = Variable + sign(Threshold - RandomNumber) * amount

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022a

Tags

Gefragt:

am 8 Sep. 2022

Beantwortet:

am 8 Sep. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by