Solved

Shuffling an array

Posted on 2001-06-17
19
385 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
 
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
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: 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

867 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

22 Experts available now in Live!

Get 1:1 Help Now