# Random with no repeats

Get help using Construct 3!

### » Sat Jun 24, 2017 7:04 am

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
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Sat Jun 24, 2017 7:09 am

check each new random number if it is already assigned and if reapeat until it's a new number.
B
43
S
22
G
15
Posts: 389
Reputation: 12,274

### » Sat Jun 24, 2017 7:15 am

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:

B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Sat Jun 24, 2017 7:20 am

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

i do not understand
B
43
S
22
G
15
Posts: 389
Reputation: 12,274

### » Sat Jun 24, 2017 7:29 am

mercuryus wrote:
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.

B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Sat Jun 24, 2017 8:01 am

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...
B
43
S
22
G
15
Posts: 389
Reputation: 12,274

### » Sat Jun 24, 2017 8:38 am

mercuryus wrote: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.
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Sat Jun 24, 2017 9:15 am

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.
B
49
S
10
G
9
Posts: 1,224
Reputation: 8,480

### » Sat Jun 24, 2017 11:41 am

Davioware wrote: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?
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Sat Jun 24, 2017 12:51 pm

B
71
S
22
G
279
Posts: 3,838
Reputation: 153,878

Next