Solved

shuffle array

Posted on 2001-07-20
6
836 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now