Random Array Index Integer Output

Hello, can anyone explain to me why the integer output of the Random Node does not match the actual default setting of the array contents?

The node will print 0, and output the 3rd element.

Or it will print 0, and output the 1st element. Etc.

It’s because you are calling it two times and it generates a random each time; one to get the ref and another for the int.

So what is the point of the integer output if it’s going to change when called?

Hello,

Let me expand a bit on what @pezzott1 said, which is correct.

In Unreal, functions can be non-pure and pure. The major difference between them is that non-pure (ones with execution pins) executes only one time per execution pin, where as pure function (Random Node) executes once per data access. This is like that by design.

In order to get your data and index to match you need to convert the random node into non-pure function. Now when you get your random from array, the data from reference will match the index. That’s because these values (outputs) are cached.

Now to answer the follow up question.

Apart from the what I described above, I believe the right use for pure function in this case would be use one or the other.
Use the reference pin if you need to work with data, use the index is you want to replace/remove/track something from the array.
Technically nothing stops you from getting a random index from pure node, then caching said index and using “Get a copy/ref” node for arrays, but at this point I think I would just use non pure node.

Hope that clears things up a bit.

2 Likes

Thank you!

I understood already what was happening, but I needed to know WHY.

Hell, ChatGPT didn’t even know this. I kid you not.

I ended up doing this, because it’s identical to RANDOM:

For me, learning why > getting the answer. Thank you for the disambiguation and @pezzott1 for the initial reach-out.

1 Like