Solved

Shuffling an array

Posted on 2001-06-17
19
390 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
[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
  • 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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

635 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