Solved

# shuffle array

Posted on 2001-07-20
836 Views
i have an array of ten digits
digitarray(10)
and i want to shuffle it
any ideas how its done?
0
Question by:amoran

LVL 22

Expert Comment

ID: 6301751
Define shuffle

regards,
CJ
0

Author Comment

ID: 6301782
currently
digitarray(10)=("1", "2", ....., "10")
i want it to become something like
digitarray(10)=("8", "4", ....., "7")
like shuffling a deck of cards

0

LVL 10

Expert Comment

ID: 6301785
heres an example of how to remove all null values from an array and condense it down,

<%

Function cleanArray(tarr)
arr = tarr
FOR i = 0 TO UBOUND(arr)
IF(arr(i) = "" AND i < UBOUND(arr))THEN
arr(i) = arr(i + 1)
arr(i + 1) = ""
END IF
NEXT
count = 0
FOR i = 0 TO UBOUND(arr)
IF(arr(i) <> "")THEN
count = count + 1
END IF
NEXT
REDIM PRESERVE arr(count)
cleanArray = arr
End Function

dim arra(6)
arra(0) = "Hello"
arra(1) = "Hi"
arra(2) = "asp"
arra(3) = ""
arra(4) = "weeeee"
arra(5) = ""

'narra will now be a clean version of arra
narra = cleanArray(arra)

FOR i = 0 TO UBOUND(narra)
Response.Write(narra(i) & "<BR>")
NEXT

%>
0

LVL 3

Expert Comment

ID: 6301950
an ideea : generate 2 random numbers and switch values in the array .. if you repeat this process for <<n>> times will will get what you want ..

rgds
0

LVL 10

Expert Comment

ID: 6301977
providing you dont reuse random numbers!, quite possible
0

LVL 3

Accepted Solution

dragosh earned 75 total points
ID: 6301990
here is some code :

<%@ Language=VBScript %>
<%Option Explicit%>

<%
Dim digitArray
Dim i

digitArray = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")

' initial vector
For i = 0 To UBound(digitArray)
Response.Write "digitArray[" & i & "] = " & digitArray(i) & "<br>"
Next

' shuffle
Dim n, k
Dim rnd1, rnd2
Dim minValue
Dim maxValue
Dim buf

minValue = 1
maxValue = 9
n = 10

' initialize generator
Randomize

For k = 1 To n

rnd1 = CInt((maxValue * Rnd) + minValue)
rnd2 = CInt((maxValue * Rnd) + minValue)

If rnd1 <> rnd2 Then
buf = digitArray(rnd1-1)
digitArray(rnd1-1) = digitArray(rnd2-1)
digitArray(rnd2-1) = buf
Else
k = k - 1
End If
Next

' shuffle vector
For i = 0 To UBound(digitArray)
Response.Write "digitArray[" & i & "] = " & digitArray(i) & "<br>"
Next

%>

hope this will help

rgds
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

JSON error 4 63
Select2 jquery help 9 88
Server Timeout with Loop 6 45
msxml3.dll error '80072efd' A connection with the server could be not established 8 39
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…