Randomly Pick a value from an array

  • How do i randomly pick a value from a given array.

    I have an array where "On Start of Layout" i "Push back" some value into the array.

    Later on in the game i would like to randomly select a value of this array.

    i have done something like below (this action is executed with the following condition)

    System countdown >= 1 (this is just a countdown timer).

    System Every 1.5 Seconds

    <img src="http://img402.imageshack.us/img402/7517/randomconstruct.png" border="0" />

    When i play the game it only displays the randomNumber.

    <img src="http://img7.imageshack.us/img7/3772/randomconstruct2.png" border="0" />

  • The array has 4 elements.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Use the & not +

  • ArcadEd

    Thanks for the hint, however it still does not solve the problem

    <img src="http://img526.imageshack.us/img526/3495/randomconstruct3.png" border="0" />

    <img src="http://img405.imageshack.us/img405/1539/randomconstruct4.png" border="0" />

    <img src="http://img706.imageshack.us/img706/2848/randomconstruct5.png" border="0" />

  • Set randomNumber to floor(random(charArray.width)). And do make sure your arrays size is not smaller than what you need.

  • What Vee41 says. Plus, make sure your array Height and Depth are at least 1, even if you are not using them. Having them at 0 caused me similar issues in the past.

  • vee41 & ArcadEd thanks for the hint. But i have found the problem. Instead of Push back, i should put it at front.

    It seems this is what happen when i use "push back"

    Array ["","","","","element","element","element"]

    So when a randomNumber generates the number 0 - 4 it will returns 0 by default because there is nothing there.

    If i use "push front" this is what happen

    Array ["element, "element", "element", ....]

    Thanks for the hint though.

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