Solved

shuffle array

Posted on 2001-07-20
6
839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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