how to random shuffle an array?

Stellaferox

Active Member
Licensed User
Hi,

I need to shuffle an array with numbers in a random way, let's say cardnumbers (in fact something else that has to do with neural networks but the same procedure).
Now I think this has to be a loop, run at least once in which the condition has to be that the array already holds the number in which case a new randomnumber is set, looping unitl this number is not yet in the array.
Like:

FOR c = Nr1 to Nr2+1
DO
x = RND(Nr1,NR2)
UNTIL (x NOT in ArrayOfNumbers())
ArrayOfNumbers(c) = x
NEXT

Or does anyone have a better solution?
tnx
Marc
 

LineCutter

Active Member
Licensed User
Longtime User
I've not got this into code yet, but I have a similar need to select from an array (in my case I want a subset of the original array)

Create 2 arrayslists: one with the ordered data, the other for the shuffled data.
Pick the nth element of the ordered arraylist (where n=rnd(size of ordered array))
Append this value to the shuffled arraylist
Remove this entry from the ordered arraylist
Repeat until the ordered array is empty.

Note that the size of the ordered arraylist shrinks, so the random number is always a valid element (& is never a duplicate). No futile loops.
 

Stellaferox

Active Member
Licensed User
This DOES call for a shuffle property in arraylist.... Erel? Also usable when developing cardgames!
Marc
 

Cableguy

Expert
Licensed User
Longtime User
I don't think this is good for an arraylist, as after randomizing we no longer know were a particular data is....I think the best solution would be to create a standart control, maybe called RandomList??

This calls for action on our beloved dll creators...:sign0188:
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
CableGuy,

You had a similar problem to this with your pairs game and as I recall I made you I snippet of code (sorry but I've lost it now :confused:) whereby I placed all the image names into a String separated with commas.
All that had to be done was randomly select any point in the String and find the first comma then remove the image name and add it into the desired position in your grid. The string is made shorter because the entry was removed and so as LineCutter put it, there's NO futile loops!

Sounds like you need a similar thing with your Array of numbers. If you want the code snippet I think it probably best to cut and paste from your pairs game.

Regards,
RandomCoder.
 

Cableguy

Expert
Licensed User
Longtime User
Thanks RandomCoder, I still have the original code stored here....I do not need it, was StellaFerox who need it...

Still a control would come in handy!
 
Last edited:

Stellaferox

Active Member
Licensed User
would be handy though, because shrinking the array still holds the possibility that several times one of the first positions is chosen randomly.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I think we may have crossed wires Stellaferox,
My solution involves randomly selecting your numbers from a Sting that is made up of all the numbers you require in the array. The numbers are placed into the array in the next available space so presuming that you clear the Array before you start, by the time you have removed each number from the String the Array will be randomly filled.

Regards,
RandomCoder.
 
Top