Solved

Shuffling an array

Posted on 2001-06-17
19
386 Views
Last Modified: 2013-12-25
Here is my problem -

i have a array namely -

@array = (av,sdf,sdfew,sdfwet,sdf,wer);

i want to shuffle the contents of the array which means :
that i want to change the order to any random one.

eg. @array = (sdf,wer,sdfwet,sdfew,sdf,av);

each time i call the script , it should give a different result.

Anant
0
Comment
Question by:anant99
  • 9
  • 8
  • 2
19 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6200463
 sub fisher_yates_shuffle {
      my $array = shift;
      my $i;
      for ($i = @$array; --$i; ) {
          my $j = int rand ($i+1);
          next if $i == $j;
          @$array[$i,$j] = @$array[$j,$i];
      }
  }

  fisher_yates_shuffle( \@array );    # permutes @array in place
0
 
LVL 1

Author Comment

by:anant99
ID: 6200948
thanks for your solution. however i have one more query . is it possible to assign a code to the particular shuffled array.

eg -
asd805r(this is just an example of code. i dont care how long or short the code is) will always generate the same @array (in the same order).

Anant
 
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6204385
not sure exactly what you want...

would something simple like this work?

 fisher_yates_shuffle( \@array );    # permutes @array in place

@thisOrderofanArray=@array;

that is the best way to do such a thing....

if I misunderstand, please let me know exactly what you want.

Bob
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 84

Expert Comment

by:ozo
ID: 6206482
perldoc -q shuffle

Is this what you mean?
$code = 123;
srand($code);
fisher_yates_shuffle( \@array );
0
 
LVL 1

Author Comment

by:anant99
ID: 6206656
i actually want this -

suppose i specify the $code eg.

@array = (av,sdf,sdfew,sdfwet,sdf,wer);
$code = "24sdfbfgw4";

then it must shuffle the array according to the code. Each time i have the same code , it should produce the same results (elements of the array should be in the same place)

when the code changes , it should shuffle the array in a differently. but each time the same code is used , the elements of the array should be in the same place

Anant

ps - the code cannot be very long. however i may have 100-200 items in the array or even 1000
0
 
LVL 84

Expert Comment

by:ozo
ID: 6206698
$code = "24sdfbfgw4";
srand unpack "%32L*",$code;
fisher_yates_shuffle( \@array );
0
 
LVL 1

Author Comment

by:anant99
ID: 6209429
but you have not connected the $code with the shuffle subroutine in anyway. i tried it and it is not working.

the array should be shuffled according to the code as mentioned previously.

Anant
0
 
LVL 84

Expert Comment

by:ozo
ID: 6209447
How did you try it?
0
 
LVL 1

Author Comment

by:anant99
ID: 6211419
i mean to say that anyhow it wont work. as they are no way related. if you did not understand my question. here it is in another style :

$code = "";
@array = (av,sdf,sdfew,sdfwet,sdf,wer);
# Your code goes here

If $code is empty , then the script should shuffle the array and produce a value for $code. If $code is not empty , then it should shuffle the array on the basis of the code. Each time the code is the same , the order of contents in the array should be the same.

Help !!!

Anant
0
 
LVL 84

Expert Comment

by:ozo
ID: 6211843
$code = "";
@array = (av,sdf,sdfew,sdfwet,sdf,wer);
srand unpack "%32L*","$code      " if $code;
fisher_yates_shuffle( \@array );
0
 
LVL 1

Author Comment

by:anant99
ID: 6213148
again , you have to relate $code and the shuffle command in some way or the either. or else what is the use of $code ... if $code is empty , then once the array is shuffled , it should specify $code...

what does srand unpack "%32L*","$code do ?

please include the entire code including the shuffle subroutine.

Anant
0
 
LVL 84

Expert Comment

by:ozo
ID: 6213175
# fisher_yates_shuffle( \@array ) :
# generate a random permutation of @array in place
sub fisher_yates_shuffle {
    my $array = shift;
    my $i;
    for ($i = @$array; --$i; ) {
        my $j = int rand ($i+1);
        @$array[$i,$j] = @$array[$j,$i];
    }
}
$code = "";
@array = (av,sdf,sdfew,sdfwet,sdf,wer);
srand unpack "%32L*","$code      " if $code;
fisher_yates_shuffle( \@array );
0
 
LVL 1

Author Comment

by:anant99
ID: 6217450
it should also print the new generated code.

please add that also.

Anant
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 6217939
# fisher_yates_shuffle( \@array ) :
# generate a random permutation of @array in place
sub fisher_yates_shuffle {
   my $array = shift;
   my $i;
   for ($i = @$array; --$i; ) {
       my $j = int rand ($i+1);
       @$array[$i,$j] = @$array[$j,$i];
   }
}
$code = "";
@array = (av,sdf,sdfew,sdfwet,sdf,wer);
code ||= rand;
srand unpack "%32L*","$code      ";
fisher_yates_shuffle( \@array );
print $code;
0
 
LVL 1

Author Comment

by:anant99
ID: 6221072
dear ozo,

i tried it and it works perfetly well. however i would like to know how it works.

what do these commands really do :-
$code ||= rand;
srand unpack "%32L*","$code";

And also , how are they automatically related to the @array ?

Anant

0
 
LVL 84

Expert Comment

by:ozo
ID: 6222315
$code ||= rand; #if $code is empty, set it to a some random value

srand #Sets the random number seed for the rand operator.
unpack "%32L*","$code   ";  #turn an arbitrary code string into an integer

my $j = int rand ($i+1); # generate a random permutation
0
 
LVL 1

Author Comment

by:anant99
ID: 6223705
so how is the $code related with the shuffling of the array ? i still dont understand that...

Anant
0
 
LVL 84

Expert Comment

by:ozo
ID: 6225388
srand seeds the random number function used by fisher_yates_shuffle
0
 
LVL 1

Author Comment

by:anant99
ID: 6227022
Thanks for the info... It works great !

Anant
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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