how to random shuffle an array?

Discussion in 'Questions (Windows Mobile)' started by Stellaferox, Jun 30, 2007.

  1. Stellaferox

    Stellaferox Active Member Licensed User


    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.

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

    Or does anyone have a better solution?
  2. LineCutter

    LineCutter Active Member Licensed 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.
  3. Stellaferox

    Stellaferox Active Member Licensed User

    Hey LineCutter, that is a neat solution!
  4. Stellaferox

    Stellaferox Active Member Licensed User

    This DOES call for a shuffle property in arraylist.... Erel? Also usable when developing cardgames!
  5. Cableguy

    Cableguy Expert Licensed 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:
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User


    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.

  7. Cableguy

    Cableguy Expert Licensed 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: Jun 30, 2007
  8. Stellaferox

    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.
  9. RandomCoder

    RandomCoder Well-Known Member Licensed 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.

  10. Stellaferox

    Stellaferox Active Member Licensed User

    I placed my solution in a new post. Somehow the code doesnt work.....
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice