adding or subtracting depending on the number

2 visualizaciones (últimos 30 días)
jason
jason el 8 de Sept. de 2022
Respondida: Walter Roberson el 8 de Sept. de 2022
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 comentario
David Hill
David Hill el 8 de Sept. de 2022
What have you tried? Can you explain yourself better through a code example?

Iniciar sesión para comentar.

Respuestas (2)

Torsten
Torsten el 8 de Sept. de 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 el 8 de Sept. de 2022
Variable = Variable + sign(Threshold - RandomNumber) * amount

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by