How do I randomly populate a 50 row, 1 dimension array with unique numbers ?

0 favourites
  • 4 posts
From the Asset Store
You think you can guess the character’s name?! Then let’s see how smart you are!
  • Hello,

    I have an array with only dimension X.

    I want to populate each of its 50 cells with a unique number from 1 to 50. I also want number 25 to falls after cell #20.

    How can I do that ?

    Tagged:

  • Here are some ideas.

    You can start the array with all 50 numbers in order. Repeat 50 times->set at (loopindex) to loopindex+1. The you can repeatedly swap values at two different locations. The shuffling would be done like this.

    vars i0,i1,tmp
    repeat 100 times
    — set i0 to int(random(50))
    — set i1 to int(random(50))
    — set tmp to array.at(i0)
    — array: set at i0 to array.at(i1)
    — array: set at i1 to tmp

    Another idea would be to take advantage of the arrays sort action to do the shuffling. You’d set the array size to 50x2, and the first column would be a random value and the second column the number. You’d populate it with:

    repeat 50 times
    — array: set at (loopindex, 0) to random(1)
    — array: set at (loopindex, 1) to loopindex+1

    Then you’d use the sort action. Then you can probably remove the random column with a delete action on the y axis.

    A third option could be to use 50 instances of a sprite as an array instead of the array object. You’d create 50 instances and you’d be able to access it like an array with sprite(n).value. To populate it you can do: for each sprite ordered by random(1) -> sprite: set value to loopindex+1.

    You also could look at the advanced random plugin since it may be designed for that sort of thing.

    To limit the value 25 to be after index 20 you’d probably want to check where that value was with the array.indexof expression, then either just re generate the array again to see if it’s better or just swap it with a value elsewhere with basically the swap idea above.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you !

    I found this tutorial that allows unique value and shuffling in only one line !

    https://www.construct.net/en/tutorials/shuffle-array-boss-easy-event-2396

    I'll consider your suggestion for filtering and adding value at the end of the array.

  • A little late, but that is exactly what the permutation table feature of the advanced random plugin is for in C3. A permutation table is a randomized set of nonrepeating numbers.

    + System: On start of layout
    -> AdvancedRandom: Create permutation with 50 values starting at 1
     // Push permutation table values to array size (0,1,1)
    ----+ System: Repeat 50 times
    -----> Array: Push back AdvancedRandom.Permutation(LoopIndex) on X axis
    
     // If the value 25 is in an index below 20, delete that index, and add 25 at a random index between 20 and the remainingwidth of the array.
    ----+ System: Array.IndexOf(25) < 20
    -----> Array: Delete index Array.IndexOf(25) from X axis
    -----> Array: Insert 25 at index floor(random(20,Array.Width)) on X axis
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)