Re: Randomly outputting an array



ryanarossi@xxxxxxxxx said:

What would be the simplest way to output an array in random order, but
every element in the array MUST be outputted and the elements cannot
repeat? My current code is below, which I simply have a very large
array and I am outputting windows of an arbitary length.

OPTION BASE 0

DIM DECK(52) AS INTEGER
DIM N AS INTEGER
DIM R AS INTEGER
DIM T AS INTEGER

N = 52

REM Populate array
FOR J = 0 TO N - 1 STEP 1
DECK(J) = J
NEXT

REM Display in original order
FOR J = 0 TO N - 1 STEP 1
PRINT J;" ";
NEXT
PRINT

REM Shuffle
FOR J = 0 TO N - 1 STEP 1
R = INT(RND * (N - J)) + J
T = DECK(R)
DECK(R) = DECK(J)
DECK(J) = T
NEXT

REM Display in shuffled order
FOR J = 0 TO N - 1 STEP 1
PRINT J;" ";
NEXT
PRINT

END

Converting to a sensible language is left as an exercise.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: Randomly outputting an array
    ... array and I am outputting windows of an arbitary length. ... DIM DECKAS INTEGER ... REM Display in original order ...
    (comp.programming)
  • Re: Randomly outputting an array
    ... array and I am outputting windows of an arbitary length. ... DIM DECKAS INTEGER ... REM Display in original order ...
    (comp.programming)
  • Re: Detecting a running process.
    ... Private Declare Function EnumProcessModules Lib "psapi.dll" _ ... (ByVal dwProcessID As Long, _ ... Dim nProcesses As Long ... 'fill an array of longs with the ...
    (microsoft.public.vb.winapi)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)