in reply to How to not print the same random number

## Assign the numbers 1-10 to array @arr @arr = 1..10; ## Pass @arr by reference to function randomize() randomize(\@arr); ## When used in this context, the array prints ## with space delimiters between items print "@arr"; sub randomize { ## Declare variables inside the scope of ## this function my ($p, $size, $swap, $key); ## Assign the array reference passed to ## the function to $p $p = $_[0]; ## Assign the length of the array pointed ## to by $p to $size. This is the number ## of items in the array - 1 $size = $#$p; ## Randomize all items up to the next to ## last item. The last item is randomized ## already if the other items are. for (0..($size-1)) { ## Pick a random item from current ## position to end $swap = int rand($size - $_ + 1) + $_; ## Swap item with current position $key = $p->[$_]; $p->[$_] = $p->[$swap]; $p->[$swap] = $key; } }
You should probably look up $_ and @_ and read up on references, it will help you understand this a bit.