Solved

shuffle array

Posted on 2001-07-20
6
833 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now