How do I generate non-repeating unique random numbers the simplest way without regex?

0 favourites
  • 4 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hi,

    Was looking around for all the solutions available on the forums and tutorials but for now I haven't found a simple enough method to do this that doesn't run multiple times until it hits the right number that doesn't exist. That seems unnecessarily wasteful.

    So I was thinking if it's possible to get non-repeating random numbers from a pool which gets smaller every random pick so we ensure the same number doesn't get picked twice. There's gotta be a way to do this without using arrays or regex.

    Tried to do it with replace and turning it to a string and back but to no avail since I can't turn a string into an expression.

    Anyone has some cool ninja ideas this could be done?

    Thank you

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could play with the pool idea by using a text variable. We'll be using it like an array.

    Here's a pool of random numbers than uses one at a time and then removes it from the pool. Only works 10 times.

    global text pool="0123456789"
    global number n=0
    global rnd=0
    every 1 seconds
    -- set n to int(random(1)*len(pool))
    -- set rnd to int(mid(pool, n, 1))
    -- set pool to left(pool, n) & right(pool, len(pool)-n-1)

    There are various ways we can adjust this.

    The list of unique numbers can be expanded to have more than single digits. We can even generated the list with events at the start of layout to make things easier.

    Another idea is to make the list reusable by adding the values back to the list. That would semi break the limit of no duplicate numbers, but maybe only allow a number to be picked again after say 50 numbers or something.

    It can be fairly tedious to emulate an array with text though.

    If the amount of random numbers is unbounded i'd just generate the random numbers and add them to a dictionary. If the number wasn't in it then it is unique, otherwise generate a new number. Seems simple enough, and fast at lookups. The caveat is this will use more and more memory over time.

    global number rnd=0
    on function "unique random"
    -- set rnd to random(1)
    ---- dictionary: doesn't have key rnd
    ------ add key rnd to dictionary
    ------ stop loop
    ------ set return to rnd
  • OK I got replies by two of one of my favourite people in this place. blackhornet R0J0hound thank you very much. I appreciate both of you and you are an incredible treasure to this community. Either you directly or your work has helped me in multiple instances.

    blackhornet That's a great plugin, I wonder why it's not a part of the default plugins of C3. Seems like a basic necessity to pick unique randoms. Thanks

    R0J0hound I tried doing it with strings and then I just search for ","&theNumberItself that way I can use decimals :p It's really interesting to me that such a basic and simple looking thing (getting a unique number) takes so much effort to get done.

    It's deceivingly complex. Our intuitive presumptions of complexity have nothing to do with reality :p

    I'll try this if plugin slaps me too hard. Thank you sir!

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