?
Solved

shuffle array

Posted on 2001-07-20
6
Medium Priority
?
844 Views
Last Modified: 2012-05-04
i have an array of ten digits
digitarray(10)
and i want to shuffle it
any ideas how its done?
0
Comment
Question by:amoran
6 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6301751
Define shuffle

regards,
CJ
0
 

Author Comment

by:amoran
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

by:makerp
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:dragosh
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

by:makerp
ID: 6301977
providing you dont reuse random numbers!, quite possible
0
 
LVL 3

Accepted Solution

by:
dragosh earned 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question