in reply to how to use conditional loops when using while loop

I would use pack and unpack here, a MAC is a hex number.

pack the start and end of range of MACs. Unpack again to get integers.
Pick random numbers in the range between the two integers then pack an unpack to hex again.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: how to use conditional loops when using while loop
by ikegami (Patriarch) on Jul 28, 2005 at 16:18 UTC
    Careful. MAC addresses require 48 bits, but many perls use 32 bits ints. Also, rand seems to only return numbers between 0 and 65536 for me. (The docs imply this can be changed when compiling perl.)

      Yeh I have been fighting with the 48/64 bit issue, I added a couple of Fs to pad the hex out to 64 bytes but I am still having problems that I guess are to do with endianisms.

      I must admit that is the reason I did not post code yet and now I have to run to the airport. Perhaps I'll get a chance to play later.

      The rand issue is solved by rand*rand or similar, I guess the requirement is not for crypto quality randomness here.

      Must fly, Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!

        A node I wrote much earlier show how to convert a MAC address from a hex string into a floating point number, since they have more bits of precision. Just be sure not to use integer operations on the number. If you can figure out how to do a large random, here's a harness:

        # IMPORTANT: # $first_mac, $last_mac, $range and $random # are floating point number potentially too # big to fit into an integer. Do not perform # integer operations on them. my $first_mac = mac_hex2num('00:96:14:00:00:00'); my $last_mac = mac_hex2num('00:96:14:2F:FF:FF'); my $range = $last_mac - $first_mac + 1; my $random = ???; my $mac = man_num2hex($first_mac + $random);