int(rand() + 1), int(rand()) +1, and hashes

Russ Schnapp rlssdpm at
Sun Jul 16 09:35:25 CDT 2000

> A floating number 1.005 is converted into a string and is used as 
a hash key.
> Converting a floating number to a string is obviously much slower 
> converting an int
> what's so surprising?

Okay, I'll bite.  Here are the code samples again:

> sub randint1 {
>         $r1 = int( rand(4) + 1 );
>         $h1{$r1}++;
> }
> sub randint2 {
>         $r2 = int( rand(4) ) + 1;
>         $h2{$r2}++;
> }

randint2() ran slower than randing1(), and you're saying that's 
because $r2 was a float while $r1 was an int?  I need to understand 
this.  How did adding an integer (1) to the result of an int() generate 
a floating result?


The posting address is: san-diego-pm-list at

List requests should be sent to: majordomo at

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at> .
This is the general rule for most mailing lists when you need
to contact a human.

More information about the San-Diego-pm mailing list