How do I generate a random IP with no duplicates

Get help using Construct 2

Post » Mon Jun 13, 2016 9:28 pm

Hi :D

I try to generate a random IPv4 for a simulation, but thats not the hardest part:
Code: Select all
floor(random(256)) & "." & floor(random(256)) & "." & floor(random(256)) & "." & floor(random(256))

My problem is, every request should generate a new random IPv4 (without duplicates) ... how do i have to do that?

Thanks ♥

EDIT:
I found a way myself, if someone have a better solution, let me know ;)
The official documentation is very handy! :lol:

EDIT2:
I could even block reserved IPs

Image
B
11
S
4
G
5
Posts: 74
Reputation: 3,435

Post » Thu Jun 16, 2016 4:05 pm

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.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Thu Jun 16, 2016 5:53 pm

gumshoe2029 wrote: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) :lol:

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

Image
EDIT:
And this will run server side, so no client performance impact.
B
11
S
4
G
5
Posts: 74
Reputation: 3,435

Post » Thu Jun 16, 2016 7:08 pm

Populate an array with sequential values and then remove an element randomly each time...
A big fan of JavaScript.
B
76
S
20
G
74
Posts: 2,249
Reputation: 46,472

Post » Thu Jun 16, 2016 7:18 pm

Colludium wrote: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.
B
11
S
4
G
5
Posts: 74
Reputation: 3,435

Post » Fri Jun 17, 2016 5:05 pm

realMantis wrote:... 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 ... :D


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.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Fri Jun 17, 2016 5:07 pm

gumshoe2029 wrote:
realMantis wrote:... 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 ... :D


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 :lol: but who knows ;)
B
11
S
4
G
5
Posts: 74
Reputation: 3,435


Return to How do I....?

Who is online

Users browsing this forum: wertt22 and 21 guests