Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > random IP

Reply
Thread Tools

random IP

 
 
Bob
Guest
Posts: n/a
 
      05-18-2004
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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-18-2004
In article <XFoqc.18062$(E-Mail Removed)>,
Bob <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-18-2004
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


 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      05-18-2004
On 05/18/04 10:28 Jürgen Exner spoke:
> 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


I meant different each time to program is run.


--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      05-18-2004
On 05/18/04 10:28 Walter Roberson spoke:
> In article <XFoqc.18062$(E-Mail Removed)>,
> Bob <(E-Mail Removed)> 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.
 
Reply With Quote
 
Richard Voss
Guest
Posts: n/a
 
      05-18-2004
Bob wrote:
> On 05/18/04 10:28 Jürgen Exner spoke:
>
>> 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

>
> 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{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>
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-18-2004

Quoth Richard Voss <(E-Mail Removed)>:
> Bob wrote:
> > On 05/18/04 10:28 Jürgen Exner spoke:
> >
> >> Bob wrote:
> >>
> >>> I need to generate an IP address based on a random number.
> >>
> >> Of course you realize that the current time is not random at all, are
> >> you?

> >
> > 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.


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.
~ http://www.velocityreviews.com/forums/(E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-18-2004
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
 
Reply With Quote
 
chris-usenet@roaima.co.uk
Guest
Posts: n/a
 
      05-19-2004
In article <XFoqc.18062$(E-Mail Removed)>,
Bob <(E-Mail Removed)> 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 <(E-Mail Removed)-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
 
Reply With Quote
 
Greg Miller
Guest
Posts: n/a
 
      05-19-2004
On Tue, 18 May 2004 19:20:06 +0000 (UTC), Ben Morrow
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random "The IListSource does not contain any datasources" and more (Crashing a live site at random, twice a week or so) Lars-Erik Aabech ASP .Net 8 04-28-2005 07:52 AM
Random not really random... Maziar Aflatoun ASP .Net 4 08-05-2004 01:26 AM
Random NOt random? Darren Clark ASP .Net 3 06-24-2004 05:23 PM



Advertisments