How do I randomise array elements?

Get help using Construct 2

Post » Tue Mar 18, 2014 4:54 pm

Hi. I'll try to keep this short but sweet. Variable names and such given here are not the same as what I'm using, and all code-like writing I've written is pretty much only pseudo, in the interest of simplicity.

I have a string, called 'myWord', which could be a word of any kind. In this example, we'll call it "JUMBLED".

I also have an array, called 'myArray', which takes each character from 'myWord' and places them in order.

So, in theory, it looks something like myArray = [J,U,M,B,L,E,D]

What I desperately need to achieve, is for the contents of myArray to be randomised, (something like myArray = [U,M,E,D,L,J,B]) instead of in the correct order, as it is now.

The problem is, despite all my research I haven't been able to find any particularly useful solution that doesn't cause a headache.

Therefore, does anybody know of a simple means to randomise the contents of an array, either when inputting the string value to the array, or after it has already been created?

Many thanks!

Paul
B
9
S
1
G
1
Posts: 11
Reputation: 1,018

Post » Tue Mar 18, 2014 5:08 pm

ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198

Post » Tue Mar 18, 2014 5:35 pm

the solution is quite simple:
1.create an additional row on myArray.
2.on the second row place your letters
3.on the first row place random numbers
4. myArray > sortX
5.read new order of letters from 2nd row
B
12
S
3
Posts: 104
Reputation: 1,655

Post » Tue Mar 25, 2014 11:44 am

Thanks for your replies, guys.

MaorKeshet, a problem I perceived with your solution was the same problem I was having with randomly moving letters based on index position: As far as I'm aware it isn't possible to randomly generate numbers up to whatever limit, without randomly picking the same number more than once. So, randomly generating numbers in a list between 0-5 for instance, rather than generating 1,5,2,4,3,0, is far more likely to always produce something like 4,4,2,1,0,0, which isn't particularly helpful. Sure I could still order that as 0,0,1,2,4,4, but ideally in my scenario (particularly as other uses for random generation have come up), I need them to be unique numbers.

The solution I've gone with in the end is to have three Arrays:

ArrayLetters [J, U, M, B, L, E, D]
ArrayTemporary [J, U, M, B, L, E, D]
ArrayRandom [0, 0, 0, 0, 0, 0]

- then, with a loop, randomly moving a letter from ArrayTemporary to ArrayRandom, and then deleting said letter from the index of Array Temporary, so that I have something like...

ArrayLetters [J, U, M, B, L, E, D]
ArrayTemporary [ ]
ArrayRandom [B, L, M, J, U, D, E]

It's by no means ideal, though it works, but I'm not certain it would work with multiple rows in the same array, given that the loop is based on the width of ArrayTemporary, which changes over the course of the loop.

Basically, as mentioned in the beginning of this post, if I knew of a way to randomly generate numbers without repeating those that have already been generated, it would make the process a hell of a lot easier.

Many thanks for your suggestions.
B
9
S
1
G
1
Posts: 11
Reputation: 1,018

Post » Tue Mar 25, 2014 11:56 am

One method I saw here and used was to have 2 local variables, and use them as temporary storage of values as you loop through the array's cells. For each array element: set variable A to current cell value, then set variable B to a random number (0,arraywidth). Then set current element to value at index of variable B, and set the value at index of variable B to variable A.

It may sound a little complicated at first, but it's really quite simple.
B
28
S
8
G
5
Posts: 429
Reputation: 7,007

Post » Tue Mar 25, 2014 7:56 pm

Last edited by Yann on Sun Sep 14, 2014 3:35 pm, edited 1 time in total.
B
68
S
22
G
14
Posts: 1,485
Reputation: 16,561

Post » Wed Mar 26, 2014 3:54 pm

Tobye, I'm yet to try your solution but I'll let you know as soon as I have whether it works or I have trouble.

Yann, your file says it was created in Construct v164.2 'or later', but the newest version I'm able to find at all is v163. Weird!
B
9
S
1
G
1
Posts: 11
Reputation: 1,018

Post » Wed Mar 26, 2014 4:00 pm

He is using the beta release
You can find it here https://www.scirra.com/construct2/releases/r164.2
It's time for me to return the favor :)
B
32
S
9
G
9
Posts: 132
Reputation: 5,375

Post » Sat Sep 20, 2014 1:13 pm

thanks @Yann your example helped me.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests