Let’s say that you want an integer inside a custom event that is random in range and you want to use it in multiple locations but you don’t want to add it as a variable inside that blueprint (as example maybe you have 100+ custom events inside the blueprint, and each has 1 or more random numbers, that would mean 100+ useless variables inside the blueprint, which would even be a nightmare for organization), if you are going to do this:
Looking at the blueprint even tho you used “the same number output”, in all of the Print String
the Random Integer in Range
is called again and will return a new integer for each use.
One way to get around this:
Call the Random Integer in Range
function, but don’t use the output where you need, instead first take the output and call the Increment
function for that number, and then from the output of the increment, call Decrement
so that you reach the number from before and use that output where you need the same number. While in the above example it doesn’t appear to make sense to call decrement right after the increment as the number would be random anyway, well you can get into the rare situation where Random Integer from Range
will give you the maximum value, and then because you incremented that by one, you’ll be outside the range you wanted and this can be especially bad in this example:
In this situation it will try to get the value from a position that doesn’t exist inside the array, as random(0, size - 1) + 1 can return the value of 3. But by using both increment and decrement, then it will never try to get a value outside the size of the array, and by using the value from that output, you can always get the value from the same position from inside the array.
If you would have done simply this:
Then you will get in both situation a random value (well there is that slim chance where the random would return same value in both cases, but if that value would have been in the thousands+, then the chance would practically disappear).
This method works with both Int
and Float
types as they both have the Increment
and Decrement
functions.
You can use it in combination with something like this:
To have a random rotator that is constant in the event (in this situation you can make the random rotator and apply it to an actor, and get that actors rotator later, and you don’t really need to make that ++ --, but it was an example of a possible combination).