random IP

Discussion in 'Perl Misc' started by Bob, May 18, 2004.

  1. Bob

    Bob Guest

    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.
    Bob, May 18, 2004
    #1
    1. Advertising

  2. In article <XFoqc.18062$>,
    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 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
    Walter Roberson, May 18, 2004
    #2
    1. Advertising

  3. 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
    Jürgen Exner, May 18, 2004
    #3
  4. Bob

    Bob Guest

    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.
    Bob, May 18, 2004
    #4
  5. Bob

    Bob Guest

    On 05/18/04 10:28 Walter Roberson spoke:
    > In article <XFoqc.18062$>,
    > 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 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.
    Bob, May 18, 2004
    #5
  6. Bob

    Richard Voss Guest

    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>
    Richard Voss, May 18, 2004
    #6
  7. Bob

    Ben Morrow Guest

    Quoth Richard Voss <>:
    > 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.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
    Ben Morrow, May 18, 2004
    #7
  8. 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
    John W. Krahn, May 18, 2004
    #8
  9. Bob

    Guest

    In article <XFoqc.18062$>,
    Bob <> 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 <-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
    , May 19, 2004
    #9
  10. Bob

    Greg Miller Guest

    On Tue, 18 May 2004 19:20:06 +0000 (UTC), Ben Morrow
    <> 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.
    Greg Miller, May 19, 2004
    #10
  11. Bob

    Ben Morrow Guest

    Quoth Greg Miller <>:
    > On Tue, 18 May 2004 19:20:06 +0000 (UTC), Ben Morrow
    > <> 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.


    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, May 20, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    442
    mikeb
    Jun 24, 2004
  2. Maziar Aflatoun

    Random not really random...

    Maziar Aflatoun, Aug 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    26,656
    Maziar Aflatoun
    Aug 5, 2004
  3. Lars-Erik Aabech
    Replies:
    8
    Views:
    822
    Lars-Erik Aabech
    Apr 28, 2005
  4. globalrev
    Replies:
    4
    Views:
    742
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,116
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page