Random with no repeats

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

    I'm trying to come up with a system where by a grid of objects are assigned a random number but to ensure that each object is a unique number with no repeats within a range.

    For example:

    8 / 3 / 9

    2 / 4 / 1

    7 / 5 / 6

    Not:

    3 / 3 / 9

    / 3 / 2

    1 / 5 / 1

  • check each new random number if it is already assigned and if reapeat until it's a new number.

  • I think I understand the principle, it's more the practical.

    My specific case is that I have a pool of 50 levels I want to randomly assign to objects in a menu, doing silly things like this just isn't working and is mental:

  • ...

    50 levels I want to randomly assign to objects in a menu... ???

    i do not understand

  • i do not understand

    Haha yeah sorry I'm being stupid.

    So I have a grid of squares like this:

    And I have 50 levels numbered 1 - 50. What I want is for each square in this grid, at the start of the game to be assigned a random level of 1 - 50, with no repeating levels.

  • Without seeing the images it's like i mentioned:

    In practical:

    #

    Use an array (size 50:1:1) and loop-assigne a random number if not already assigned before.

    -> capx (C2)

    hope it helps...

  • Without seeing the images it's like i mentioned:

    In practical:

    #

    Use an array (size 50:1:1) and loop-assigne a random number if not already assigned before.

    -> capx (C2)

    hope it helps...

    Thanks a bunch for this, never used an array before so will study this.

  • That's not a good solution, even though it'll work in this case (since you only have 50). With larger sizes you'll endlessly hang the cpu as it regenerates and checks the numbers. What you need to do is store all your numbers in a list, (1,2 ... 500) pick a random index from the list, and return the number at that index. Then remove the number at that index from the list and repeat. This way the operation completes in a known time.

  • That's not a good solution, even though it'll work in this case (since you only have 50). With larger sizes you'll endlessly hang the cpu as it regenerates and checks the numbers. What you need to do is store all your numbers in a list, (1,2 ... 500) pick a random index from the list, and return the number at that index. Then remove the number at that index from the list and repeat. This way the operation completes in a known time.

    Good shout, what's a list though?

    Like this?

  • That's not a good solution, even though it'll work in this case (since you only have 50). With larger sizes you'll endlessly hang the cpu as it regenerates and checks the numbers. What you need to do is store all your numbers in a list, (1,2 ... 500) pick a random index from the list, and return the number at that index. Then remove the number at that index from the list and repeat. This way the operation completes in a known time.

    +1 to this method. It is nice and simple.

    If you aren't comfortable working with arrays, you could even do this with sprites to which you have assigned a number for the instance variable:

    1. Use "pick random instance", store the number assigned to that instance in an array, then delete that sprite instance.

    2. Repeat until all of the instances have been destroyed.

    I recommend learning how to do this with arrays though, as creating sprites for the sole purpose of destroying them is kind of wasteful.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the advice everyone, very helpful.

  • Array is where you set your numbers, here 0-4, but you can go to as many as you like, then the sub-event goes randomly through that array and populates Array2. After each pick, selected number is removed from the first array, so there are no repeats.

    Array is set to 5,1,1 to start with ( You can use push to add more numbers later )

    Array2 is set to 0,1,1

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