in reply to how to use conditional loops when using while loop
In the second snippet, $cnt++ is only executed when $cnt is not 4. Learn how to indent properly to avoid these errors.
The arguments for rand are off by one. Random returns a number from 0 (inclusively) to arg (exclusively). You need 48 and 256. Better yet, use hex (0x30 and 0x100) since it's more meaningful here.
I have no idea what if ($rand < 5) { $rand += 2; ... is all about. If you want to avoid '00' and '01', use
rand(0x100 - 2) + 2</p> <p>Why not just use</p> <c> sub mac { return join ':', '00:96:14', sprintf('%02X', rand(0x30)), sprintf('%02X', rand(0x100)), sprintf('%02X', rand(0x100)); }
or
sub mac { return sprintf('00:96:14:%02X:%02X:%02X', rand(0x30), rand(0x100), rand(0x100), ); }
A loop isn't appropriate here due to the lack of commonality between the octects
|
|---|