View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default random permutation of an array

You could try this

Sub Shuffle()
Dim myarray(6)
For x = 1 To 6
myarray(x) = x
Debug.Print myarray(x)
ActiveSheet.Cells(x, 1).Value = myarray(x)
ActiveSheet.Cells(x, 1).Offset(, 1).Value = Rnd
Next
ActiveSheet.Range("A1:B6").Sort Key1:=Range("B1"), Order1:=xlAscending
For x = 1 To 6
myarray(x) = ActiveSheet.Cells(x, 1).Value
Next
ActiveSheet.Range("A1:B6").ClearContents
End Sub

Mike

"Shatin" wrote:

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