Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shuffling an array

Posted on 2001-06-17
19
Medium Priority
?
392 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

688 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