1. Junior Member
Join Date
Dec 1969
Posts
29

I&#039;ve modified a function that I got from 4guysfromrolla that randomizes an array (by changing values randomly between the indexes), so that I could randomize an array I got with getrows(). The problem is that the function doesn&#039;t work the way I want it to, and I know exactly what the problem is but I can&#039;t solve it.<BR><BR>The values in the second dimension&#039;s indexes needs to preserve their relations, but the randomizer runs once for every index in the second dimension and reorders their values. I hope someone understands what I mean (I know I&#039;m not the best explainer). :-)<BR><BR>To sum up, the randomized values needs to be the same for every run through the second loop below. How do I solve this?<BR><BR>- - - - - - - - <BR><BR>Function ReArrange(ByVal arrArray)<BR><BR> intUpper1 = UBound(arrArray,1)<BR> intLower1 = LBound(arrArray,1)<BR> intUpper2 = UBound(arrArray,2)<BR> intLower2 = LBound(arrArray,2)<BR><BR> Randomize Timer<BR><BR> For x = intLower1 to intUpper1<BR> For j = intLower2 to intUpper2<BR> &#039;Slumpar fram ett arrayindex att byta med originalet.<BR> intSwapPos = Int(Rnd * (intUpper2 + 1))<BR><BR> &#039;Byter värden mellan slumpat index och originalindex.<BR> varTmp = arrArray(x,j)<BR> arrArray(x,j) = arrArray(x,intSwapPos)<BR> arrArray(x,intSwapPos) = varTmp<BR> Next<BR> Next<BR> ReArrange = arrArray<BR>End Function<BR><BR>- - - - - - - -

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## You are doing TOO MUCH!

You are scrambling *BOTH* the columns AND the rows!<BR><BR>No, no, no!<BR><BR>[ And a minor thing: In VBS, LBound is *always* zero, so no need to code for it. ]<BR><BR>Anyway:<BR><BR>Function ReArrange(ByVal arr)<BR><BR>&nbsp; &nbsp; maxColumn = UBound(arr,1)<BR>&nbsp; &nbsp; maxRow = UBound(arr,2)<BR><BR>&nbsp; &nbsp; Randomize <BR><BR>&nbsp; &nbsp; For row = 0 To maxRow<BR>&nbsp; &nbsp; &nbsp; &nbsp; swapRow = Int(Rnd * (maxRow + 1))<BR>&nbsp; &nbsp; &nbsp; &nbsp; &#039; now you must swap ALL COLUMNS in a pair of rows!<BR>&nbsp; &nbsp; &nbsp; &nbsp; For col = 0 TO maxCol<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = arr(col,row)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr(col,row) = arr(col,swapRow)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr(col,swapRow) = temp<BR>&nbsp; &nbsp; &nbsp; &nbsp; Next<BR>&nbsp; &nbsp; Next<BR>&nbsp; &nbsp; ReArrange = arr<BR>End Function<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Incidentally...

Unless you really need to keep the array around in its original form, you can probably improve performance this way:<BR><BR>&#060;%<BR>SUB ReArrange( ByREF arr ) &#039; SUB, not Function! ByREF not byval<BR><BR>&nbsp; &nbsp; maxColumn = UBound(arr,1)<BR>&nbsp; &nbsp; maxRow = UBound(arr,2)<BR><BR>&nbsp; &nbsp; Randomize <BR><BR>&nbsp; &nbsp; For row = 0 To maxRow<BR>&nbsp; &nbsp; &nbsp; &nbsp; swapRow = Int(Rnd * (maxRow + 1))<BR>&nbsp; &nbsp; &nbsp; &nbsp; &#039; now you must swap ALL COLUMNS in a pair of rows!<BR>&nbsp; &nbsp; &nbsp; &nbsp; For col = 0 TO maxCol<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = arr(col,row)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr(col,row) = arr(col,swapRow)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr(col,swapRow) = temp<BR>&nbsp; &nbsp; &nbsp; &nbsp; Next<BR>&nbsp; &nbsp; Next<BR><BR>End Sub<BR>%&#062;<BR><BR>No just invoke it via<BR>&#060;%<BR>ReArrange some2Darray<BR>%&#062;<BR>and the array will be rearranged "in place", thus saving the copy that is necessitated by using ByVal.<BR><BR><BR><BR>

4. Junior Member
Join Date
Dec 1969
Posts
29

## RE: Incidentally...

A couple of minutes after I posted the question, it came to me and I managed to creat the following function that is working. However, I don&#039;t really need two copies so your suggestion to use "ByRef" seems like the best, but couldn&#039;t I just change "ByVal" to "ByRef" and that&#039;s it? Why should I make it a SUB? Anyway, here&#039;s my function so far:<BR><BR>&#060;%<BR>Function ReArrange(ByRef arrArray)<BR><BR> intUpper1 = UBound(arrArray,1)<BR> intUpper2 = UBound(arrArray,2)<BR><BR> Randomize Timer<BR><BR> For j = 0 To intUpper2<BR> &#039;Slumpar fram ett arrayindex att byta med originalet.<BR> intSwapPos = Int(Rnd * (intUpper2 + 1))<BR><BR> For x = 0 To intUpper1<BR> &#039;Byter värden mellan slumpat index och originalindex.<BR> varTmp = arrArray(x,j)<BR> arrArray(x,j) = arrArray(x,intSwapPos)<BR> arrArray(x,intSwapPos) = varTmp<BR> Next<BR> Next<BR> ReArrange = arrArray<BR>End Function<BR>%&#062;

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Because...

&#062; Why should I make it a SUB? <BR><BR>Because of this one line:<BR>&nbsp; &nbsp; &nbsp; ReArrange = arrArray<BR><BR>You might want to read this article (all THREE parts, please):<BR><BR>http://www.4guysfromrolla.com/webtech/032801-1.shtml<BR><BR>When you copy an array in VBScript, you do a *deep* copy, meaning you eat memory and time. Avoid copying arrays if you can. And, yes, coding "ByVal" causes a copy, too.<BR><BR>

6. Junior Member
Join Date
Dec 1969
Posts
29

## RE: Because...

I tried making it a sub like this, but for some reason I got loads of duplicates.<BR><BR>&#060;%<BR>Sub Rearrange(ByRef arrArray)<BR><BR> intUpper1 = UBound(arrArray,1)<BR> intUpper2 = UBound(arrArray,2)<BR><BR> Randomize <BR><BR> For j = 0 To intUpper2<BR> &#039;Slumpar fram ett arrayindex att byta med originalet.<BR> intSwap = Int(Rnd * (intUpper2 + 1))<BR> <BR> For x = 0 To intUpper1<BR> &#039;Byter värden mellan slumpat index och originalindex.<BR> varTmp = arrArray(x,j)<BR> arrArray(x,j) = arrArray(x,intSwapPos)<BR> arrArray(x,intSwap) = varTmp <BR> Next<BR><BR> Next<BR>End Sub<BR>%&#062;<BR><BR>And then I invoke it like this<BR><BR>&#060;%<BR> arrQuest = rsQuest.GetRows()<BR> ReArrange arrQuest<BR><BR>%&#062;

7. Junior Member
Join Date
Dec 1969
Posts
29

## I got it

Forgot to change the variant name in the second loop (intSwapPos was supposed to be intSwap). Thanks for all the help!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•