How do I pick a random value in an array

0 favourites
  • 9 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • So i have and array that is made of 0 and 1, and I want to pick one random 1 value and turn it into 2. So how do you do that?

  • Domse65

    To be clear, your array has only 2 values? If so, an easy way is to use choose():

    set at value at choose(0,1) to 2

    Or you have several values of 0 and 1 and you only want to choose a random 1 value and turn it into 2?

  • Domse65

    To be clear, your array has only 2 values? If so, an easy way is to use choose():

    set at value at choose(0,1) to 2

    Or you have several values of 0 and 1 and you only want to choose a random 1 value and turn it into 2?

    I have several values of 0 and 1 and I only want to choose a random 1 value and turn it into 2

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Domse65

    Got it!

    This might not be the best way, but it's the best I have at this moment:

    Create a second Array(Array2) to store the X location on your original Array where the value 1 might be found.

    Then set value at Array2.At(floor(random(0, Array2.Width))) to 2.

    Here is a capx to help:

    https://www.dropbox.com/s/8x9qi6acmomaj ... .capx?dl=0

  • EDIT: Removed Link.

    As mentioned furthur along in the thread, this method is actually pretty terrible in terms of efficiency and should not be used :p

    Psuedocode:

    While a 1 exists in the array, pick a random array cell between 0 and Array.Width.

    Does the random cell contain a 1? If it does, add one to it and stop the loop.

    Otherwise, repeat.

    If you want to expand this to a 2d or 3d array, you'll use a similar method, but randomly pick XYZ coordinates instead. Not recommended because you will no longer be able to use IndexOf to confirm if the target value exists in the array, and can end up with an endless loop.

  • Domse65

    Got it!

    This might not be the best way, but it's the best I have at this moment:

    Create a second Array(Array2) to store the X location on your original Array where the value 1 might be found.

    Then set value at Array2.At(floor(random(0, Array2.Width))) to 2.

    Here is a capx to help:

    https://www.dropbox.com/s/8x9qi6acmomaj ... .capx?dl=0

    Could you do an example with a two dimensional array? Because I need it for a two dimensional array

  • Domse65

    I used oosyrag 's approach to solving the problem, but applied it to a 2d array:

    https://www.dropbox.com/s/5orwwxtwyrvdm ... .capx?dl=0

  • Domse65 Note:

    The first code from SimoneT is the better and a more performant way -> save the positions of the nr. 1 in a new Array.

    --> save the position in a 2d-array: first 'line' -> x-position in array secont-line -> y-position

    Because worst case:

    You have a large array but only 1 position for the right entry. It could take to a 'endless loop' because a random number be able occur multiple times.

    ... save the performance!

  • That makes a lot of sense. Here is the better one, for a two dimensional array.

    https://www.dropbox.com/s/1cj0u7ipiioao ... .capx?dl=0

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