# Random Number Without Repeat Problem

Discussion and feedback on Construct 2

### » Fri Sep 12, 2014 11:56 pm

New to construct, first time posting on the forums:

I have a game, which On Collision with "DoorWarp"(an invisible sprite object) the game sets a variable RoomRandom from 1-10, this part is working great. Depending on the value it retrieves, it loads a new layout based on the value. Level 1-1 for 1, 1-2, for 2, etc.
I'm making a rogue-like-esque-lite game where level progression should be random.
Each 'level' is on it's own layout, and is associated with a RoomRandom number as explained before.

How do i make it so RoomRandom doesn't repeat numbers when you collide with DoorWarp.

For example, i can be on 1-1 and hit the DoorWarp, loading 1-5... Perfect.
I beat 1-5 and RoomRandom chooses a new number, if this number is 1, it loads 1-1.... Undesired.
Sometimes, even, i beat 1-5 and RoomRandom lands on 5 again and reloads 1-5.... Undesired.

How do i make it so once RoomRandom picks a number, that number can no longer be chosen again until game over.
B
4
Posts: 6
Reputation: 626

### » Sat Sep 13, 2014 2:47 am

Easy Peasy and classic coding. There are a few ways to do this. Personally Like strings because of the small object it represents. but we will go with arrays.

You have an array with size = NumRooms
Repeat NumOfRooms
> Array.Set(0) to loopindex

boom you have your list of rooms. Next step and here is the magic.

// get a random index from remaining rooms
var index = floor( Random( 0, Array.Size ) )

// get the room number from the array at index
var roomToLoad = Array.Get( index )

// remove the value at index position so it doesn't happen again.
Array.RemoveAt( index )

You have now mastered Construct 2

As a warning. This is Psuedo Contrsuct 2 terms. I could have the terms wrong. Such as RemoveAt could in fact could be PopAt, DeleteAt... or something else. Also i think think Array.Size should probably be Array.Width. C2 naming is kinda wierd.
B
94
S
18
G
9
Posts: 2,457
Reputation: 15,167

### » Sat Sep 13, 2014 11:49 am

I was going to suggest something else, but @Jayderyu example is more elegant and graceful. What he is suggesting to do is perfect.
Kurieus
Come visit and play Blast Box!
B
15
S
3
Posts: 205
Reputation: 1,349

### » Sat Sep 13, 2014 3:52 pm

oh by the way. You can also put entire Layout names as "strings" in side the array instead of just numbers. Works really well.
B
94
S
18
G
9
Posts: 2,457
Reputation: 15,167

### » Tue Sep 16, 2014 5:22 am

thanks guys! you're the best
B
4
Posts: 6
Reputation: 626

### » Tue Sep 16, 2014 5:28 am

actually would you perhaps be able to link a capx example, because i'm having trouble understanding whats really going on here, i can't even find the stuff in c2 editor to do any of this
B
4
Posts: 6
Reputation: 626

### » Tue Sep 16, 2014 6:09 am

like how would i implement this into loading a layout, and what is this loopindex, i'm just really confused.
B
4
Posts: 6
Reputation: 626

### » Tue Sep 16, 2014 6:17 pm

Nevermind, i figured it out! thanks a ton.
B
4
Posts: 6
Reputation: 626

### » Fri Oct 10, 2014 7:12 am

Can someone explain how? I have 4 layouts. And they go to each layout randomly by code "Layout " & ceil(random(3) + 1). And it's working. But the problem is, I want each layout to appear once.
B
4
S
1
Posts: 10
Reputation: 370