random IP

B

Bob

I need to generate an IP address based on a random number.
How would I take the current time "1084887295" and put "." every two
characters and throw away any digits left after I use 8?

So keep the last 8 and put a "." every 2.
 
W

Walter Roberson

:I need to generate an IP address based on a random number.
:How would I take the current time "1084887295" and put "." every two
:characters and throw away any digits left after I use 8?

:So keep the last 8 and put a "." every 2.

time

Returns the number of non-leap seconds since
whatever time the system considers to be the epoch
(that's 00:00:00, January 1, 1904 for Mac OS, and
00:00:00 UTC, January 1, 1970 for most other systems).

That's usually a 32 bit number, which is the same number of bits
as for an IPv4 address. But just in case it isn't...

my @t = unpack "C*", pack "L", time;
my $ip = join ".", @t[-4..-1];
 
J

Jürgen Exner

Bob said:
I need to generate an IP address based on a random number.
How would I take the current time "1084887295" and put "." every two
characters

perldoc -q comma: "How can I output my numbers with commas added?"
and throw away any digits left after I use 8?

perldoc -f substr

Of course you realize that the current time is not random at all, are you?

jue
 
B

Bob

perldoc -q comma: "How can I output my numbers with commas added?"




perldoc -f substr

Of course you realize that the current time is not random at all, are you?

jue

I meant different each time to program is run.
 
B

Bob

:I need to generate an IP address based on a random number.
:How would I take the current time "1084887295" and put "." every two
:characters and throw away any digits left after I use 8?

:So keep the last 8 and put a "." every 2.

time

Returns the number of non-leap seconds since
whatever time the system considers to be the epoch
(that's 00:00:00, January 1, 1904 for Mac OS, and
00:00:00 UTC, January 1, 1970 for most other systems).

That's usually a 32 bit number, which is the same number of bits
as for an IPv4 address. But just in case it isn't...

my @t = unpack "C*", pack "L", time;
my $ip = join ".", @t[-4..-1];

Thanks.
 
R

Richard Voss

Bob said:
I meant different each time to program is run.

Today's computers can start a program more than once within a second. (Seriuos!)
You might get closer to unique values using more exact times than just seconds.
Time::HiRes provides a time() function that returns floats. Still, that's not
strictly unique.

HTH
 
B

Ben Morrow

Quoth Richard Voss said:
Today's computers can start a program more than once within a second. (Seriuos!)
You might get closer to unique values using more exact times than just seconds.
Time::HiRes provides a time() function that returns floats. Still, that's not
strictly unique.

Include the pid, $$, and sleep for at least a second. Unless you are
using threads, the combination (host, time, pid) will then be unique.

Also, I would MD5 it, just to make it less deterministic.

Ben
 
C

chris-usenet

I need to generate an IP address based on a random number.

Are there any "business" restrictions on your generated IP address? For
example, would 127.0.0.1 be acceptable? What about 192.168.255.255?


Walter Roberson said:
my @t = unpack "C*", pack "L", time;
my $ip = join ".", @t[-4..-1];

This will generate a different 4-octet value for each second. These are
not necessarily valid IP addresses (but nor is putting a dot between
every two decimal digits of the current time in seconds, as suggested
by the OP).

Chris
 
G

Greg Miller

Include the pid, $$, and sleep for at least a second.

If all of the processes sleep for 1 second, then this has no
effect on the randomness. You could sleep for a random period of
time, but if you've got several processes starting every second, then
you're still guaranteed to have duplicates as long as your time
resolution is only one second. And with only 32 bits to work with,
you're going to have dups quite often anyway.
 
B

Ben Morrow

Quoth Greg Miller said:
If all of the processes sleep for 1 second, then this has no
effect on the randomness.

Randomness, no; uniqueness, yes. If you sleep for a second then (time,
pid) is unique during that second.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top