Re: Randomly outputting an array



Richard Heathfield wrote:
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.

#!/usr/bin/perl
use warnings;
use strict;

sub shuffle {
my $deck = shift;
my $i = @$deck;
while ( $i-- ) {
my $j = int rand( $i + 1 );
@$deck[ $i, $j ] = @$deck[ $j, $i ];
}
}

# Populate array
my @deck = 0 .. 51;

# Display in original order
print "@deck\n";

# Shuffle
shuffle \@deck;

# Display in shuffled order
print "@deck\n";

__END__



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: "how to split a string in a random way"
    ... I created an integer array and I pass it along with a ... number to Factorize(). ... This time I send array1 through the Shuffle() method just prior to sending ... Dim array3 As Integer= Utility.GenArray ...
    (microsoft.public.dotnet.languages.vb)
  • Re: round-robin an arraylist
    ... Of all of them the work on the Array is the fastest. ... RemoveAt(), Addparadigm to do the shuffle is only slightly slower than the ... public class RoundRobin { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Card dealing and random repetition
    ... inspiring for an 'elegant' way to simulate a shuffle. ... The dealer divide the deck in two parts, array ... left-half-deck (here there is a slight probability of 2 card staying ...
    (comp.programming)
  • Re: Elementary but surprisingly difficult.
    ... the original message from Albert-- the one I first replied to-- described the problem as removing duplicates from an array. ... Now I hate to get all Computer Science on everyone, but the very definition of array denotes an ordering; you can say the "first" item in an array, and it has clear and unambiguously meaning. ... So no, if preserving the original order mattered, "bdeca" wouldn't work, because the original order had a "a" as the *first* element. ... When a duplicate item is found, the "read" pointer is advanced past the item and the write pointer is left where it is. ...
    (comp.lang.forth)
  • Re: randomly choose some uniq elements of an array
    ... >> Why do you think it makes sense to shuffle the whole array in order to ... > whole array (i.e. do a length-changing splice) in order to pick three ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)