How do I generate non-consecutive numbers.

  • I would like to know how to not generate sequential numbers in 3, exemple, "10,11,12" "57,58,59"... but possible "10,12,13" "34,35,37"

  • Is there a specific range? Also, is the offset always 1?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Is there a specific range? Also, is the offset always 1?

    can be random, but not with 3 consecutive numbers.

  • Ok...

    Let's say you want three non-consecutive numbers between 0 and 99.

    You could try something like this:

    z = random(100)
    y = random (z - 1))
    x = random(y - 1)
    [/code:jjh1mz0u]
    
    This will return a sequence in which z>y>x, that is, they're in ascending order from x to z.
    
    Hope this helps. Cheers!
  • Ok...

    Let's say you want three non-consecutive numbers between 0 and 99.

    You could try something like this:

    > z = random(100)
    y = random (z - 1))
    x = random(y - 1)
    [/code:12z3j0dd]
    
    This will return a sequence in which z>y>x, that is, they're in ascending order from x to z.
    
    Hope this helps. Cheers!
    

    That would give me sequential numbers... I need some way that blocks this kind of sequence.

    Exemple:

    10 random numbers (0,30)

    1 - 5 - 9 - 12 - 13 - 14 - 25 - 27 - 29 - 30

    12 - 13 - 14 They are sequenced, and this couldnt happen.

  • The example provided is correct because the x - 1 guarantees that they are descending in order and never numbers that are next to one another. Did you try it? Your own example is incorrect : random 0,30 would never generate 30, it's generate x to y, but not y.

  • You could do something like this to generate it in one go. It's also if you don't care about the upper bound.

    array: set size to (0,1,1)
    
    set v0 to -1
    set v1 to -1
    set v2 to -1
    
    repeat 10 times
    --- set v2 to v1
    --- set v1 to v0
    --- if v2 +1 = v1 
    ------ add int(random(2,10)) to v0
    --- else
    ------ add  int(random(1,10)) to v0
    --- array: push v0 to back[/code:3q6v6xg8]
    
    But if you want all the values to be within a range you can do the following.  Note: this will hang if you can't possibly have 'num' values without 3 consecutive values below 'maxVal'
    Anyways, barring any typos.
    [code:3q6v6xg8]array: set size to (0,1,1)
    var num =10
    var maxVal = 30
    
    repeat num times
    --- array: push int(random(maxVal)) to back
    
    array: sort
    
    //// get rid of duplicates by making consecutive
    var shift=0
    for "" from 1 to num-1
    --- add shift to array.at(loopindex)
    --- if array.at(loopindex) = array.at(loopindex-1)
    ------ add 1 to shift
    ------ add 1 to array.at(loopindex)
    
    /// get rid of three consecutive numbers sequences
    set shift to 0
    for "" from 2 to num-1
    --- add shift to array.at(loopindex)
    --- if array.at(loopindex) = array.at(loopindex-1)+1
    --- and if array.at(loopindex-1) = array.at(loopindex-2)+1
    ------ add 1 to shift
    ------ add 1 to array.at(loopindex)
    
    //// shift stuff back down so the last number in within the maxVal
    var i=0
    while
    array.at(num-1) >= maxVal
    --- set i to int(random(1, num))
    --- if array.at(i) > array.at(i-1)+1
    --- and if array.at(i-1) =/= array.at(i-2)+1
    --- and for "" from n to num-1
    ------ subtract 1 from array.at(loopindex)[/code:3q6v6xg8]
    
    edit:
    bruno's example would fail if z ended up being 0.  Also it could still give three consecutive numbers.
  • The example provided is correct because the x - 1 guarantees that they are descending in order and never numbers that are next to one another. Did you try it? Your own example is incorrect : random 0,30 would never generate 30, it's generate x to y, but not y.

    I just tested our friend's example.

    But if I put random (0,100) and the first result is 56 per example, only numbers will be generated below that, I lose the rest of the numbers.

  • I just tested our friend's example.

    But if I put random (0,100) and the first result is 56 per example, only numbers will be generated below that, I lose the rest of the numbers.

    Yeah, but the 56 itself is random... It's just an anchor from which all numbers are created. You're not "losing" anything, it's just that randomness picked them out that time.

    bruno's example would fail if z ended up being 0. Also it could still give three consecutive numbers.

    You're right, I missed that. Sorry.

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