For Loop in Unreal Engine Limited to 40 Iterations Despite Random Integer in Range Up to 100

Hi everyone,

I’m having an issue with my Blueprint in Unreal Engine 5.5 and could use some help:

In my project, I’m generating a building with a variable number of floors. To do this, I’m using the Random Integer in Range node, with the Min value set to 10 and the Max value set to 100. The result is used as the Last Index in a For Loop to create the floors.

Problem:

Although the Random Integer in Range node outputs correct values between 10 and 100 (confirmed via Print String), the For Loop never executes more than 40 iterations. Even when I print the current index inside the loop, it always stops at 40, even if the Last Index is set to 100.

Steps I’ve tried:

  1. Tested Random Integer in Range Node: Works perfectly and outputs values up to 100 as expected.
  2. Checked Last Index before the For Loop: The value is correctly passed from the Random Integer in Range node.
  3. Manually Set Last Index: When I manually set the Last Index to 100, the loop runs as expected and creates 100 floors.
  4. Printed Index Inside the Loop: The loop only iterates up to 40, even when the Last Index is greater.

Additional Details:

  • The First Index of the loop is set to 1, and the Last Index comes directly from the Random Integer in Range node.
  • There are no visible clamps, branches, or conditions limiting the value of the Last Index.

I’m completely stumped as to why the loop seems to cap at 40 iterations, despite the Last Index being correctly set. Has anyone encountered a similar issue or have an idea of what could be causing this?

Thanks in advance for your help!

Hey @Tangier!

This sounds like something that could be a tiny overlooked detail causing a big problem. If you get a random value below 40, does it still go to 40?

If you don’t mind, bring us some snips showing all pertinent code so we can take a look at it!

Get back to us soon!

Hi, thanks for the quick reply!

I tested what happens when the maximum random value is below 40. For example:

If I set the Random Integer in Range to Min = 1 and Max = 10, I get random values between 1 and 10 as expected, and the loop runs up to those values.

However, when the Max is set to 100, the loop still never goes beyond 40, even though the random value is correctly generated (confirmed with Print String).

  1. I’ve attached the following screenshots:
    [Screenshot of the Blueprint]

  2. [Output Log showing the Print String output directly from the Random Integer in Range]

3.[Output Log showing the Print String output from the For Each Loop]

Additionally, I tried using an Array containing numbers from 1 to 100 instead of the Random Integer in Range node. Unfortunately, the For Loop still caps at 40 iterations, even when accessing values beyond 40 from the array.
Interestingly, if I manually input the Last Index as a fixed value (e.g., 100), it works perfectly and creates the correct number of iterations.

Do not connect pure functions to the loop macros, as they will get called on every loop
That means that the random int will get called on every loop, so on every loop a new max will be generated
You should cache the result of the random to a local var and use that as the max in your loop

1 Like

Zeaf is correct. You need to cache the value


A quick test confirms this

Set the Floors value as a variable.
Random from range → Set Floors → Loop (floors)


Other option is to set the floors in construct and build on Begin Play.


Another option is to Loop on tens and use Delay Until Next Tick. This will have to run in the event graph.

Thank you so much for pointing this out – it saved me a lot of time and frustration. I really appreciate your help, guys!

1 Like