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.
0 个评论
采纳的回答
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
braceletOfStrength
hammerOfThunder
none
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
braceletOfStrength
hammerOfThunder
none
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!