Azzera filtri
Azzera filtri

Why is it doing all 100 attempts as a singular go?

1 visualizzazione (ultimi 30 giorni)
I am working on a bit of code to generate a the probability of an item dropping. I have it set so that its going through an if loop chain, where if it isnt one thing it goes to the next thing and if it isnt that it goes to the next and so forth, but when I try to run it with a for loop to dictate how many times it needs to run it just gives all of the attempts to a singular output giving that one output a probability of 100% each run, which isnt whats supposed to happen.
numOfSims = 100;
potionOfStrength = 0;
braceletOfStrength = 0;
hammerOfThunder = 0;
none = 0;
X = rand;
X2 = rand;
X3 = rand;
for iteration_num = 1:numOfSims
if X<0.4
potionOfStrength = potionOfStrength +1;
else
if X2<0.2
braceletOfStrength = braceletOfStrength +1;
else
if X3<0.2
hammerOfThunder = hammerOfThunder +1;
else
none = none +1;
end
end
end
This is the code I've been working on.

Risposta accettata

Voss
Voss il 18 Mag 2024
Modificato: Voss il 18 Mag 2024
Your code does the same thing 100 times because the random values don't change from one iteration to the next.
I guess you would want to generate new random X, X2, and X3 in each iteration of the for loop:
numOfSims = 100;
potionOfStrength = 0;
braceletOfStrength = 0;
hammerOfThunder = 0;
none = 0;
for iteration_num = 1:numOfSims
X = rand;
X2 = rand;
X3 = rand;
if X<0.4
potionOfStrength = potionOfStrength +1;
else
if X2<0.2
braceletOfStrength = braceletOfStrength +1;
else
if X3<0.2
hammerOfThunder = hammerOfThunder +1;
else
none = none +1;
end
end
end
end
potionOfStrength
potionOfStrength = 36
braceletOfStrength
braceletOfStrength = 16
hammerOfThunder
hammerOfThunder = 6
none
none = 42
Or make X, X2, and X3 random vectors of length numOfSims (i.e., 100), and index them inside the loop:
numOfSims = 100;
potionOfStrength = 0;
braceletOfStrength = 0;
hammerOfThunder = 0;
none = 0;
X = rand(1,numOfSims);
X2 = rand(1,numOfSims);
X3 = rand(1,numOfSims);
for iteration_num = 1:numOfSims
if X(iteration_num)<0.4
potionOfStrength = potionOfStrength +1;
else
if X2(iteration_num)<0.2
braceletOfStrength = braceletOfStrength +1;
else
if X3(iteration_num)<0.2
hammerOfThunder = hammerOfThunder +1;
else
none = none +1;
end
end
end
end
potionOfStrength
potionOfStrength = 37
braceletOfStrength
braceletOfStrength = 13
hammerOfThunder
hammerOfThunder = 13
none
none = 37
In either case, you can utilize elseif to shorten your code, e.g.:
for iteration_num = 1:numOfSims
X = rand;
X2 = rand;
X3 = rand;
if X<0.4
potionOfStrength = potionOfStrength +1;
elseif X2<0.2
braceletOfStrength = braceletOfStrength +1;
elseif X3<0.2
hammerOfThunder = hammerOfThunder +1;
else
none = none +1;
end
end
or:
for iteration_num = 1:numOfSims
if X(iteration_num)<0.4
potionOfStrength = potionOfStrength +1;
elseif X2(iteration_num)<0.2
braceletOfStrength = braceletOfStrength +1;
elseif X3(iteration_num)<0.2
hammerOfThunder = hammerOfThunder +1;
else
none = none +1;
end
end
  2 Commenti
Larose
Larose il 19 Mag 2024
I completely forgot I hadn't moved it to be nested in the for loop, thank you so much, my brain is shot lol

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by