SOLVED - How do you loop through an array assigning a random int to each int?

I am trying to create an int array and assign random numbers from 1-4 to it.
So the end result could be something like:
{1, 3, 3, 1, 4}
{2, 3, 1, 4, 2}
{4, 1, 2, 4, 4}

I am running into the error “This invocation calls a function that has the ‘decides’ effect, which is not allowed by its context.”
But when I add the < decides > I get a different error of “Function is invoked with incorrect failure bracketing style.” when calling StartFunction()

Any help appreciated, thank you!

        StartFunction() # when I add decides below this line shows an error

        Print("=StartFunction called=", ?Duration := 20.0)

        MyArray : []int = array{0, 0, 0, 0, 0}
        for (Index := 0..MyArray.Length-1):
            set TempRandomInt = GetRandomInt(1,4)
            if (Element := MyArray[Index]):
                MyArray[Index] = TempRandom #this line is the error
                Print("{Element} in MyArray at index {Index}", ?Duration := 30.0)
                Print("Random num = {TempRandom} ", ?Duration := 30.0)

Thank you

Define your array as a var like so:

var MyArray : []int = array{1, 2, 3, 4, 5}

Then in your function:

set MyArray = Shuffle(MyArray)

This will overwrite MyArray with a randomized array. :smiley:

1 Like

This is a very elegant solution, when you said random it gave me this idea which works nicely also :slight_smile:
Thank you

MyArray : []int = array{GetRandomInt(1,4), GetRandomInt(1,4), GetRandomInt(1,4), GetRandomInt(1,4), GetRandomInt(1,4)}
        for (Index := 0..MyArray.Length-1):
            if (Element := MyArray[Index]):
                Print("{Element} in MyArray at index {Index}", ?Duration := 30.0)
1 Like

In the future, the reason you were getting the error is because it needs to be if( set MyArray[Index] = TempRandom):. This is because MyArray[Index] has the potential to fail if the index was bigger than the array’s size, so the if prevents an out of bounds error.

Edit: just saw you already seemed to know that anyway, just missed it while writing that one line