Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Array Scramble

I have a 1D array with 40 elements. Element 1 = 1, 2 = 2 etc. I wan
to scramble the numbers within the array. So I want all 40 number
still but scrambled. What is the easiest way to do this?

Dim RandomColumnArray (40) As Variant

'Load Array with numbers from 1 to 40
For X = 1 To ColumnCount
RandomColumnArray(X) = X
Next X

'Scramble Array
???????

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Array Scramble

ExcelMonkey,

Assuming you're just using values from 1 to 40....
This doesn't scramble a prefilled array. It's just an array filling routine.

If you want a true scrambler, then let me know...


Sub testit()
Const cQuantity = 40
Dim i As Long, j As Long, r As Long
Dim arr(1 To cQuantity) As Long

For i = 1 To cQuantity
r = Int((cQuantity * Rnd) + 1)
Do
For j = 1 To i - 1
If arr(j) = r Then Exit For
Next
If j < i Then r = r + 1
If r cQuantity Then r = 1
Loop Until j = i
arr(i) = r
Next

For i = 1 To cQuantity
Cells(i, 1).Value = arr(i)
Next

End Sub


Rob


"ExcelMonkey " wrote in message
...
I have a 1D array with 40 elements. Element 1 = 1, 2 = 2 etc. I want
to scramble the numbers within the array. So I want all 40 numbers
still but scrambled. What is the easiest way to do this?

Dim RandomColumnArray (40) As Variant

'Load Array with numbers from 1 to 40
For X = 1 To ColumnCount
RandomColumnArray(X) = X
Next X

'Scramble Array
????????


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Array Scramble

Here is a generalized function for shuffling a 1D long array:

Public Function ShuffleArray(varr)

'
' Algorithm from:
' The Art of Computer Programming: _
' SemiNumerical Algorithms Vol 2, 2nd Ed.
' Donald Knuth
' p. 139
'
'
Dim List() As Long
Dim t As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim lngTemp As Long

t = UBound(varr, 1) - LBound(varr, 1) + 1
ReDim List(1 To t)
For i = 1 To t
List(i) = varr(i)
Next
j = t
Randomize
For i = 1 To t
k = Rnd() * j + 1
lngTemp = List(j)
List(j) = List(k)
List(k) = lngTemp
j = j - 1
Next
ShuffleArray = List
End Function

This shows how to call it:

Sub Main()
Dim varr()
Dim varr1
ReDim varr(1 To 40)
For i = 1 To 40
varr(i) = i
Next
varr1 = ShuffleArray(varr)
Range("A1:A40").Value = Application.Transpose(varr1)
End Sub


--
Regards,
Tom Ogilvy


ExcelMonkey wrote in message
...
I have a 1D array with 40 elements. Element 1 = 1, 2 = 2 etc. I want
to scramble the numbers within the array. So I want all 40 numbers
still but scrambled. What is the easiest way to do this?

Dim RandomColumnArray (40) As Variant

'Load Array with numbers from 1 to 40
For X = 1 To ColumnCount
RandomColumnArray(X) = X
Next X

'Scramble Array
????????


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Array Scramble

Thank-you Tom. Much appreciated.

RK


---
Message posted from http://www.ExcelForum.com/

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scramble a number RobN[_2_] Excel Discussion (Misc queries) 8 April 22nd 09 04:44 PM
how can i scramble data for a sweepstakes? Ro Excel Worksheet Functions 2 October 28th 08 07:06 PM
Hi--how do I scramble a list randomly? lmcshelp Excel Worksheet Functions 1 November 1st 06 06:34 AM
Word Scramble [email protected] Excel Programming 9 December 10th 03 04:36 AM
Scramble names zxc[_4_] Excel Programming 1 August 31st 03 05:09 PM


All times are GMT +1. The time now is 05:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"