How do I generate a sequence of random numbers, where no two consecutive numbers are ever the same.

0 favourites
  • 5 posts
From the Asset Store
This is a Dungeon Master tool & the 1st of 12 Combat Engines from the Building Combat Engines for Browser Games workshop
  • I want 3 different enemies to appear in a random order, but never two the same after each other.

    - By choosing one at random, it often happens that the same enemy appears even three or more times in a row.

    - With an advanced random permutation table

    (-> AdvancedRandom: Create permutation with 3 values starting at 0)

    that I shuffle every 3 'appearances', I often get the same enemy two times in a row (example of a sequence: 0, 1, 2, 2, 1, 0, 0, 2, 1, 1, 2, 0).

    - So when shuffling the permutation table I check if the last enemy (of the first table) and the first enemy (of the new table) are not the same. Else I reshuffle.

    However, I feel that this is a very complex and far-fetched solution. Can it also be simpler?

    [functions]

    | Local static number index‎ = 0

    ----* On function 'getNext' → Number

    --------+ System: index < 2

    ---------> Functions: Set return value AdvancedRandom.Permutation(index)

    ---------> System: Add 1 to index

    --------+ System: Else

    --------+ System: index = 2

    ---------> Functions: Set return value AdvancedRandom.Permutation(2)

    ---------> Functions: Call randomizeTable

    ---------> System: Set index to 0

    | Local number last‎ = 0

    ----* On function 'randomizeTable'

    -----> System: Set last to AdvancedRandom.Permutation(2)

    -----> AdvancedRandom: Shuffle permutation table

    --------+ System: While

    --------+ System: last = AdvancedRandom.Permutation(0)

    ---------> AdvancedRandom: Shuffle permutation table

    Thanks!

    Wim

  • I'm wondering how you could ever get it to appear 3 times in a row? Seems like twice would be the most possible. I think your approach is fine though just reshuffle if it's the same, that would also be my first approach to this.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If there are only tree possible values, then a permutation table is an overkill. Here is a much easier solution:

    | Global number num‎ = 0
    
    + System: Every tick
    -> System: Set num to num=0 ? choose(1,2) : num=1 ? choose(0,2) : choose(0,1)
    -> Text: Append num & " "
    
  • dop2000, off course, such an easy solution, I was making it much to difficult.

    There's still a small chance of getting long repetition of two of the same numbers: 1,2,1,2,1,2,3 but this doesn't seem a real problem.

    lionz: 3 times in a row is only a problem when simply picking a random number every time. Ex. 10 x choose(1,2,3) -> 3,1,2,1,3,3,3,3,1,2

    Thank you both!

  • For a larger set of data, I would use an array, with an instance variable to store the picked value. Upon randomly selecting an index, delete it out of the array, and store the value in the instance variable. You can push it back into the array when the next value is chosen.

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