Vladimir Kotal writes:
> random(3C) returns long which is 4 bytes in /usr/bin/nc (nc is delivered 
> only as 32-bit prog).

Actually, the random(3C) man page indicates that it returns a value in
the range 0 to 2^31-1.  In other words, it's a 31 bit non-negative
value, and it doesn't matter how the application is compiled.

> This means (random() & 0xFFFF) will be just the 2 
> less significant bytes of the 4 returned by random().

Yes, but why?

> The purpose of the expression on line 792 is preparation for permuting 
> the numbers in (lo,hi) interval. It seems that the goal of the binary 
> operation is to cap the random number returned from random() to 16 bits 
> (so it fits in the range of usable TCP/UDP ports) and then further limit 
> it with the range 'hi - lo'.

That sounds wrong to me.  This code is tossing away 15 bits of the
randomness, and (much worse yet) is biasing the results.  The first
65536 - (65535 MOD (hi - lo + 1)) values will be hit preferentially,
something like (1 + 65536 / (hi - lo + 1)) times as often.  That's
possibly an annoying skew, if the range is large, and it happens
precisely because the random value is truncated before being used for
the modulo (%) selection.

> Although without the binary operation this will remain functional 
> (thanks to the modulo operation), IMHO the idea is logical and should be 
> preserved. There is probably a better way how to do it, though. (cast to 
> in_port_t ?)

I don't actually understand the logic behind it.  I would have thought
that it was just randomizing over a range, but it seems to have some
inner logic that escapes me.  I don't think it can be right.

Perhaps someone thought that random(3C) was able to return negative
numbers?

-- 
James Carlson, Solaris Networking              <[EMAIL PROTECTED]>
Sun Microsystems / 35 Network Drive        71.232W   Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757   42.496N   Fax +1 781 442 1677
_______________________________________________
opensolaris-code mailing list
opensolaris-code@opensolaris.org
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to