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($minhex); # 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;