Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

what does rand[] return?

by si_lence (Deacon)
on Nov 30, 2005 at 11:17 UTC ( #512902=perlquestion: print w/replies, xml ) Need Help??
si_lence has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
I came across a (at least to me) unexpected behaviour of the rand function. If I use my $num = rand[10]; I get back numbers like 863651.70263671, 1592604.921875, 1865907.1796875, 29093.6875.
Now I know that I should have called it as my $num = rand(10); but I was surprised that it a) works (even under strict) and b) what values I got back.
Can somebody please explain to me what happens here? perldoc -f rand did not help

Replies are listed 'Best First'.
Re: what does rand[] return?
by Perl Mouse (Chaplain) on Nov 30, 2005 at 11:23 UTC
    rand takes an optional argument. The argument you give it is [10], which is a reference to an (anonymous) array. Now, rand uses its argument as if it were a number. Perl happily casts types for you, and if use a reference (in this case [10]) as a number, you get its memory address. Which is a pretty big number. If you give rand a big number as argument, you get back a random number from 0 up till that big number.

    And that's exactly what you are seeing.

    Perl --((8:>*
      Thanks for the quick answer
      Makes perfect sense (now)
Re: what does rand[] return?
by secret (Beadle) on Nov 30, 2005 at 11:29 UTC

    Well if you do ;

    print 0 + [10] ;
    you will see that perl treats the reference to the anonymous array [10] as a number . So what you do is like :
    rand number

    In my case this number is 27783332 and the array reference is 0x1a7f0a4 ( that you can see by doing print [10]) which is 27783332 in hex ...

    If i repeat execution i get the same reference over and over again for this anon array. I don't know why but that's another problem ;)

      If i repeat execution i get the same reference over and over again for this anon array. I don't know why

      I am as ignorant as you are, but if I was perl,
      I would make life easy and return the next possible free address that was big enough to store your anonymous array reference. If you freed that up - no longer use it - and asked for a new one, I would lazily give you just the one that I had before.

      Cheers, Sören

      If i repeat execution i get the same reference over and over again for this anon array. I don't know why but that's another problem ;)

      I'm not an internals guy but I would guess this is an intentional optimization, though Happy-the-monk may also be correct. It would rely on perl noticing the following things:

      • the contents of this array are all constants
      • no other references to this array are taken (i.e. its refcount drops to zero at the end of the loop)
      • the array is not modified in any way
      Given all that, perl might allocate the array only once and intentionally reuse the same reference on each iteration.

      One way to find out might be to modify the loop so it creates a second array reference of the same size and then does something with it like store a reference somewhere, and see if that first array still gets the same address every time.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://512902]
Approved by GrandFather
[Corion]: A good daypart!
Corion feels slightly bad for initiating a discussion yesterday and then running away. I guess I should write that up as a meditation or SoPW
[Corion]: Not the "running away" part but the question+ discussion about IO-less HTTP modules

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2016-12-08 08:57 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (137 votes). Check out past polls.