Nick Gilbert
Nick Gilbert

Reputation: 4240

Simple Perl Syntax Explanation

I'm trying to translate a function from Perl which I've never programmed in to Java. I understand the code except for this line.

srand(time() ^($$ + ($$ <<15))) ;

I believe that srand is like Random.nextInt() in Java but I have no clue what $$ + $$ means in Perl nor the $$ << 15. I'm sure this is probably simple Perl syntax but I can't find a simple explanation.

Line in context

#!/usr/bin/perl
srand(time() ^($$ + ($$ <<15))) ;
for ($x=0;$x<10;$x++) {
print rand() . "\n";
}

Upvotes: 1

Views: 95

Answers (1)

AKHolland
AKHolland

Reputation: 4445

srand seeds the random number generator. This is similar to new Random(seed) in Java. rand() is more similar to Random.nextInt(). There isn't really any good reason to do this, as it will be called implicitly (and probably with a better seed) the first time rand() is used. The stuff inside is basically just trying to get some random-ish data using the PID.

Upvotes: 4

Related Questions