How can I select a random variable from an array?

0 favourites
  • 10 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Can someone please help me with the following?

    I would like to know how to select a random Instance variable stored in an array.

  • If the array is 1d and has the length 10 ...

    Set value --> arrayname.At(int(random(10)))

    10 can be changed to the array size/length expression..


    This get the value from a randon cell in the array... Is that what you want?

  • Joe7 showed how to handle a 1 dimensional array. Sqiddster showed how to handle a 2 dimensional array. Here is an example that also handles a three dimensional array.

    Actually, it has 3 arrays: one dimension, two dimension, and three dimension. It loops through all dimensions of the arrays inserting a number into each "cell" and then when the mouse is clicked, retrieves a value from a random "cell" in each array.


  • Thank you for your replies. It's a 1d array do I will use Joe7's answer.

    Thans staan!

  • If I have a 2 dimensional array that stores 0s and 1s, how can I pick random 1?

  • See the first answer above.

  • Umh, I get a random cell. But what I want is a random cell from the cells that contain value "1".

    I tried just picking random cells and see if it contains value 1 or not, but since it is in "while" loop and does not get 1, it goes nuts and crashes.

  • Ah.. I was going to ask if you want a 'random one' or a 'random 1'

    You could loop through the array, and when you have a '1' you add the array index to a temporary array. Then use the above method on that array to get an index into the first array.


  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • What if you want to select a random element, but don't know what the length of the array is?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)