# How do I randomly place numbers in an array?

Get help using Construct 2

### » Wed Feb 22, 2017 5:01 pm

I am wanting to create an array that has shuffled numbers in 6 different fields. I know how to choose a random number, but I don't know how to use that number only once.

This is what I am trying to achieve.

R = 4
O = 1
Y = 3
G = 2
B = 6
V = 5

R = 1
O = 5
Y = 4
G = 6
B = 2
V = 3
B
47
S
20
G
6
Posts: 120
Reputation: 6,718

### » Wed Feb 22, 2017 5:23 pm

You would need to keep track of each random number selected in its own variable and then check the new random number against each of those to make sure you are not getting repeated numbers.

Looks like you need 6 variables and call them R1,R2 etc....

Looks like Tom has a tutorial:

https://www.scirra.com/tutorials/315/generating-random-numbers-without-duplicates/revisions/2089
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

### » Wed Feb 22, 2017 6:35 pm

Perfect! That is exactly what I needed!

Thank you
B
47
S
20
G
6
Posts: 120
Reputation: 6,718

### » Wed Feb 22, 2017 6:41 pm

The 6 variables are not the best solution (is not scalable). You can search directly in the array before setting a new random value with Array condition "Contains Value".
B
37
S
7
G
5
Posts: 222
Reputation: 5,625

### » Wed Feb 22, 2017 7:32 pm

Matthew de wrote:Perfect! That is exactly what I needed!

Thank you

You are very welcome!
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

### » Wed Feb 22, 2017 8:53 pm

B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

### » Thu Feb 23, 2017 6:41 am

I think Tom's tutorial will work the best for me.

However, I am actually needing to randomly select from a specific set of numbers instead of 1-6. I need to use 0,2,4,6,8, and 10.

The tutorial sets the range like this...
System - Set RandomNumber to floor(Random(1,7))

How can I randomly use my specific numbers instead of a range?
B
47
S
20
G
6
Posts: 120
Reputation: 6,718

### » Thu Feb 23, 2017 7:20 am

floor(Random(0,6)) * 2
B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

### » Thu Feb 23, 2017 5:46 pm

I actually woke up this morning with a solution immediately in my head. I should have used CHOOSE instead of RANDOM in this case.

I am now having difficulty with getting it to work with the number zero though...
This works:
floor(Random(1,6))*2
or
choose(2,4,6,8,10)

But this does not:
floor(Random(0,6))*2
choose(0,2,4,6,8,10)

The crazy thing is, when I make some changes to allow text instead of numbers it works.
choose("0","2","4","6","8","10")
Even though the numbers were entered via text, will this cause any problems? Should I just go with this?
B
47
S
20
G
6
Posts: 120
Reputation: 6,718

### » Thu Feb 23, 2017 5:58 pm

Matthew de wrote:I actually woke up this morning with a solution immediately in my head. I should have used CHOOSE instead of RANDOM in this case.

I am now having difficulty with getting it to work with the number zero though...
This works:
floor(Random(1,6))*2
or
choose(2,4,6,8,10)

But this does not:
floor(Random(0,6))*2
choose(0,2,4,6,8,10)

The crazy thing is, when I make some changes to allow text instead of numbers it works.
choose("0","2","4","6","8","10")
Even though the numbers were entered via text, will this cause any problems? Should I just go with this?

0 multiplied by 0 is 0
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

Next