How do I generate a random IP with no duplicates

0 favourites
  • 7 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hi

    I try to generate a random IPv4 for a simulation, but thats not the hardest part:

    floor(random(256)) & "." & floor(random(256)) & "." & floor(random(256)) & "." & floor(random(256))[/code:1q0xyufz]
    My problem is, every request should generate a new random IPv4 (without duplicates) ... how do i have to do that?
    
    Thanks ?
    
    [b]EDIT:[/b]
    I found a way myself, if someone have a better solution, let me know 
    The official  documentation is very handy!   
    
    [b]EDIT2:[/b]
    I could even block reserved IPs
    
    [img="https://dl.dropboxusercontent.com/u/11035294/Scirra.com/request_random_IP.png"]
  • When you generate an IP, add it to an array and compare each time you generate a new one in order to throw out copies.

    This will become very inefficient though if you are generating large numbers of these things.

  • When you generate an IP, add it to an array and compare each time you generate a new one in order to throw out copies.

    This will become very inefficient though if you are generating large numbers of these things.

    I already do that (see Screenshot)

    I did a Benchmark to proof you wrong. I ran a test with a simple project to measure the time it takes to generate a new IP with over 200,000 IPs in a array. Btw. Browsers are effected by vsync (NWjs too) ...

    ... So after 200,000 generated IPs in the array, it takes 0.0083s on my 120hz Monitor to generate a new IP, that is unique. But ... the normal delta-time for a 120hz monitor is all so 0.0083s per tick.

    On my Intel i5 4570 with 3.20ghz, NWjs has a CPU usage of ~0.9% after generating over 200,000 IPs ... so no real performance impact.

    Btw. an 1dArray with exactly 200,000 IPs would have a physical size of 4.2mb ...

    EDIT:

    And this will run server side, so no client performance impact.

  • Populate an array with sequential values and then remove an element randomly each time...

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Populate an array with sequential values and then remove an element randomly each time...

    Then the array would by very huge in file size. Nobody knows how many user would play this game in the long term, but every player needs 1-10 IPs, so i had to generate a very big amount of them. And the performance impact would be no different.

    Btw. I'll assign every IP a Username and other informations, so with my solution, i can do this in one go without the need of two arrays.

  • ... So after 200,000 generated IPs in the array, it takes 0.0083s on my 120hz Monitor to generate a new IP, that is unique. But ... the normal delta-time for a 120hz monitor is all so 0.0083s per tick.

    On my Intel i5 4570 with 3.20ghz, NWjs has a CPU usage of ~0.9% after generating over 200,000 IPs ... so no real performance impact.

    Btw. an 1dArray with exactly 200,000 IPs would have a physical size of 4.2mb ...

    RIght, by "large number", I am thinking of millions or billions. Not 200,000. :-p The likelihood of you getting collisions with 200,000 is extremely small.

  • > ... So after 200,000 generated IPs in the array, it takes 0.0083s on my 120hz Monitor to generate a new IP, that is unique. But ... the normal delta-time for a 120hz monitor is all so 0.0083s per tick.

    >

    > On my Intel i5 4570 with 3.20ghz, NWjs has a CPU usage of ~0.9% after generating over 200,000 IPs ... so no real performance impact.

    >

    > Btw. an 1dArray with exactly 200,000 IPs would have a physical size of 4.2mb ...

    >

    RIght, by "large number", I am thinking of billions or trillions. Not 200,000. :-p

    i dont think, that i will have that much players ... ever

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)