How do I come up with a random sequence of unique numbers?

Just started using Construct 2? Post your questions here

Post » Sun Mar 12, 2017 9:30 am

I need to arrange the items in my array in a random sequence. How do I come up with a random sequence?

Eg: In my game, I will need to arrange 24 items. If only have 5 items to arrange, sample random sequences are:

3-2-5-4-1
1-4-2-3-5
2-5-1-4-3

I believe that if I can have a way to random list the number 1 to 24 in a non-repeating sequence, I can arrange the items following the sequence. However, someone may have an even simpler suggestion.

(I'm new and have been looking and trying things out but no luck so far...)
B
11
S
2
G
1
Posts: 10
Reputation: 1,095

Post » Sun Mar 12, 2017 10:11 am

To generate random numbers in a variable you can use the Set Variable command and instead of putting a number in use the code "Random(START,END)".
So presuming you want random numbers between 0 and 5, you would use
Code: Select all
Random(0,5)

However, this will generate any random float between 0 and 5, so it allows for things like 0.1, 1.5, 2.3, etc.
to combat this you can add an additional parameter in.
Code: Select all
round(random(0,5))

this will round the float up to a whole number.
0.4 becomes 1.
1.5 becomes 2.
2.3 becomes 3.
If you want to round the number down you can use:
Code: Select all
floor(random(0,5))

This will floor the number.
0.4 becomes 0.
1.5 becomes 1.
2.3 becomes 2.
In order to check if there is any repeats, you could store the generated number in a separate variable and then run a check after each number generation to see if it exists, if it does, then re-run the generation code.

As an example, this is how you would set a text box to a random array value.
Image

Hope that helps.
B
9
S
2
Posts: 69
Reputation: 833

Post » Sun Mar 12, 2017 2:34 pm

ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Mon Mar 13, 2017 2:25 am

Thanks, Matt and blackhornet.
B
11
S
2
G
1
Posts: 10
Reputation: 1,095


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest