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

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

采纳的回答

Voss
Voss 2024-5-18
编辑:Voss 2024-5-18
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 个评论
Larose
Larose 2024-5-19
I completely forgot I hadn't moved it to be nested in the for loop, thank you so much, my brain is shot lol

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by