Re: random permutation of an array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



The following is a generalized shuffling routine that I have posted in the past over in the compiled VB newsgroups, but it works fine in the VBA world of Excel as well. Give it an array of elements and it will put them in random order and return the randomized elements back in the original array that was passed to it. It only visits *each* array element *once* so it is quick. The code takes care of running the Randomize statement one time only (which is all that is necessary).

Sub RandomizeArray(ArrayIn As Variant)
Dim X As Long
Dim RandomIndex As Long
Dim TempElement As Variant
Static RanBefore As Boolean
If Not RanBefore Then
RanBefore = True
Randomize
End If
If VarType(ArrayIn) >= vbArray Then
For X = UBound(ArrayIn) To LBound(ArrayIn) Step -1
RandomIndex = Int((X - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
TempElement = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(X)
ArrayIn(X) = TempElement
Next
Else
'The passed argument was not an array
'Put error handler here, such as . . .
Beep
End If
End Sub

After passing your array into the RandomizeArray subroutine, its elements will be randomly reordered. The passed array may be of any normal type -- integer, string, single, etc. The neat thing is, if you pass an already randomized array to this routine, those randomly ordered elements will be randomize -- sort of like shuffling an already shuffled deck of cards.

Rick


"Shatin" <lk1439-newsgroup@xxxxxxxxxxxx> wrote in message news:OPSPIQ06IHA.4596@xxxxxxxxxxxxxxxxxxxxxxx
Can anyone help me with a script that would shuffle the elements of an array of integers randomly. For example:

MyArray before shuffling: 1, 2, 3, 4, 5
MyArray after shuffling: 3, 5, 4, 2, 1

I want the reordering to be really random.

TIA

.



Relevant Pages

  • Re: A second beginners question
    ... I am working on shuffling my array. ... > basics of shuffling the array, ... > close CONTROL2; ... then in the very next line destroy @newgroup by closing the block. ...
    (perl.beginners)
  • Re: Randomly outputting an array
    ... That was a stupid question, ... there are lots of ways of shuffling an array that ... for shuffling an array and I realized why it's structured exactly the ... Algorithm P given by Professor Knuth in Seminumerical Algorithms, ...
    (comp.programming)
  • Re: Random number again
    ... The "fill the array section" fills an array so that ... It basically randomly picks two items in the array and swaps them. ... With 12 numbers, doing 20 swaps should do a pretty good job shuffling, even if occasionally, the same slots got swapped. ... Dim raynumis just creating an array that can hold 12 different numbers. ...
    (microsoft.public.powerpoint)
  • Re: Q: Algorithm M vs. P of Knuths book
    ... > a PRNG) in some sense more random by shuffling it through ... > other hand, if one has an array filled with the numbers, ... > of the array elements with algorithm P and then output ... We might consider the entropy of an element's output position ...
    (sci.crypt)
  • Re: Newbee Question about random numbers
    ... Bob, you are almost there. ... Shuffling is not picking random numbers out of a hat -- you got that. ... This is a school project that I'm working on. ... > the words in array mixed up. ...
    (comp.lang.java.programmer)