For loop firing an random amount of times?

I have a very simple for loop setup to get instance count of an HISM and use a random stream to get a random amount, then for loop through it. For some reason print strings reveal the loop is adding far more loops then necessary or straight up completing way early, not using all of them.

The array is not being edited during the loop, there are no delays and the function isn’t called somewhere else to mess up the isntances.

By this image the loop should go for a total of 12 times:
0-11 on the instance count, then loop through each one starting from 0. But by the strings we can see it stops at 6, why only half?

By this image the loop should go for a total of 5 times:
0-4 on the instance count, then loop through each one starting from 0. But by the strings we can see it stops at 5, why an extra?

1 Like

Okay I solved it myself.

It seems since the get random in in range from stream is a “pure function” every time it is called in the loop, it gets new instance of the number; hence it not aligning.

The solution is to make it a set variable and put that in the for loop, interesting behavior.

2 Likes

yes, that’s a common misconception.
pure functions are evaluated on get. and the for-loop is a macro. so it gets it multiple times. (imho that’s a bug in the engine, since i think could be fixed with a local variable like you’ve done).
great work finding that out!
(and also be careful in the future when using/creating pure functions, keep this in mind)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.