I know I'm a little late on this thread, seeing as how there are already viable answers, but I have an alternate solution. Instead of worrying about generating each byte individually, why not generate the number as a whole:
my $pre = '00:96:14:'; my ($minhex,$maxhex) = qw(0 2fffff); # Randomize a number between $minhex and $maxhex, inclusive # Accounts for a min value other than 0 my $hostiddec = int(rand(hex($maxhex) - hex($minhex) + 1)) + hex($minh +ex); # Convert to hex again $hostidhex = unpack("H*",pack("i",$hostiddec)); # Format, getting rid of leading 0's and adding colons $hostidhex =~ s/^0*(\w{2})(\w{2})(\w{2})$/$1:$2:$3/; # Output (return here rather than print if you're in a subroutine) print $pre . $hostidhex;
I don't know why it seems clunky to me to generate the bytes individually. For instance, I find it easier and more efficient to int(rand(16)) and convert it to binary than int(rand(2)) 4 times and concatenate it. Seems to me like your output would be more varied, but that's probably just me. Mathematically it seems like it would be the same (16 possibilities for the former, 2*2*2*2=16 possibilities for the latter), but I just feel like the former offers more variety. When you only have 2 choices, even when repeating it 4 times, I just see repetition of the final pattern being more common. I'll stop babbling now.

In reply to Re: how to use conditional loops when using while loop by wink
in thread how to use conditional loops when using while loop by pcrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.