Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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
Thanks
si_lence

Comment on what does rand[] return?
Select or Download Code
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)
      si_lence
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://512902]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-10-22 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls