Solved

Shuffling an array

Posted on 2001-06-17
19
382 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
Comment Utility
 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
Comment Utility
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
Comment Utility
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
Comment Utility
perldoc -q shuffle

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

Author Comment

by:anant99
Comment Utility
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
Comment Utility
$code = "24sdfbfgw4";
srand unpack "%32L*",$code;
fisher_yates_shuffle( \@array );
0
 
LVL 1

Author Comment

by:anant99
Comment Utility
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
Comment Utility
How did you try it?
0
 
LVL 1

Author Comment

by:anant99
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
$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
Comment Utility
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
Comment Utility
# 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
Comment Utility
it should also print the new generated code.

please add that also.

Anant
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
Comment Utility
# 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
Comment Utility
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
Comment Utility
$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
Comment Utility
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
Comment Utility
srand seeds the random number function used by fisher_yates_shuffle
0
 
LVL 1

Author Comment

by:anant99
Comment Utility
Thanks for the info... It works great !

Anant
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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

12 Experts available now in Live!

Get 1:1 Help Now