Home > Archive > PERL Miscellaneous > May 2004 > random IP
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| 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.
--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
| |
| Walter Roberson 2004-05-18, 11:32 am |
| In article <XFoqc.18062$qJ5.471120@news20.bellglobal.com>,
Bob <bob.lockie.NOSPAM@mail.com> wrote:
: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];
--
Is "meme" descriptive or perscriptive? Does the knowledge that
memes exist not subtly encourage the creation of more memes?
-- A Child's Garden Of Memes
| |
| Jürgen Exner 2004-05-18, 11:32 am |
| Bob wrote:
> 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
| |
|
| On 05/18/04 10:28 Jürgen Exner spoke:
> Bob wrote:
>
>
>
> 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.
--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
| |
|
| On 05/18/04 10:28 Walter Roberson spoke:
> In article <XFoqc.18062$qJ5.471120@news20.bellglobal.com>,
> Bob <bob.lockie.NOSPAM@mail.com> wrote:
> :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.
--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
| |
| Richard Voss 2004-05-18, 2:32 pm |
| Bob wrote:
> On 05/18/04 10:28 Jürgen Exner spoke:
>
>
> 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
--
sub{use strict;local$@=sub& #123;select($,,$,,$,,pop)};unshift@_,(45
)x 24,split q=8==>
55.52.56.49.49.55.56.49.49.53;do{print map(chr,@_[0..(@_/2-1)]),"\r";$@->(1/6)=>
push@_=>shift}for@_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,'u!`onuids!Qdsm!'.
'i`bjds') #my email-address is reversed! <http://fruiture.de>
| |
| Ben Morrow 2004-05-18, 4:32 pm |
|
Quoth Richard Voss <erutiurf@web.de>:
> Bob wrote:
>
> 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
--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
| |
| John W. Krahn 2004-05-18, 5:32 pm |
| Bob wrote:
>
> I need to generate an IP address based on a random number.
use Socket;
print inet_ntoa inet_aton int rand ~0;
John
--
use Perl;
program
fulfillment
| |
| chris-usenet@roaima.co.uk 2004-05-19, 6:31 am |
| In article <XFoqc.18062$qJ5.471120@news20.bellglobal.com>,
Bob <bob.lockie.NOSPAM@mail.com> wrote:
> 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 <roberson@ibd.nrc-cnrc.gc.ca> suggested:
> 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
| |
| Greg Miller 2004-05-19, 7:31 pm |
| On Tue, 18 May 2004 19:20:06 +0000 (UTC), Ben Morrow
<usenet@morrow.me.uk> wrote:
>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.
| |
| Ben Morrow 2004-05-19, 9:31 pm |
|
Quoth Greg Miller <gmiller@NOTforSPAM.gregmiller.net>:
> On Tue, 18 May 2004 19:20:06 +0000 (UTC), Ben Morrow
> <usenet@morrow.me.uk> wrote:
>
>
> 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
--
Although few may originate a policy, we are all able to judge it.
- Pericles of Athens, c.430 B.C.
ben@morrow.me.uk
|
|
|
|
|