Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a redirect script operating on a website I created. It sends 1/3 of visitors to site A, 1/3 of visitors to site B and 1/3 to site C.

It works by dividing the last part of the users IPv4 address (0-255) by 3, and they are redirected to site A, B or C depending on the remainder. So far site A has had 109 visitors, site B has had 85 and site C has had 101.

Am I correct in assuming that the last segment of the IPv4 address is assigned completely at random?

Also, is the distribution of numbers for the last segment equal?

share|improve this question

migrated from webmasters.stackexchange.com May 12 '12 at 15:18

This question came from our site for pro webmasters.

What makes you think that the last segment is "assigned completely at random"? –  w3d May 11 '12 at 12:02
It's a guess. I am unsure if this guess is correct. That's the content of my question. –  Adjam May 11 '12 at 16:07
Why not just do round robin? –  Bigbio2002 May 15 '12 at 15:31
@Bigbio2002 Because users might come back. I don't want a user to be sent to the good version, then the poor version, as this will effect user behaviour. –  Adjam May 18 '12 at 13:01

2 Answers 2

up vote 14 down vote accepted

IPv4 addresses are not quite so clumsy and random as you would assume.

Firstly, an IPv4 address is separated into 4 octets, each ranging from 0 to 255. However, this is not entirely the case as 0 is a reserved number for the network and 255 is reserved for broadcast, which really limits you to 1-254.

Secondly, you have to consider that IPv4 addresses are assigned in blocks to organizations and ISPs who then subdivide them based on a subnet address that delineates which bits in the octet represent the network and which bits represent the individual host. So:

Hosts provided: 65534

Would mean you could have addresses ranging from to Because of this:

Three separate hosts, same network, but same last octet:

This means you can't always count on the last octet to be 'random'.

Thirdly, the means of IP address assignment is not always random. Most home users will get a public IP address assigned by their ISP via DHCP, where their endpoint will get an address picked by a computer (usually sequentially, first come/first served from an available pool) upon which they hold a lease for a predetermined amount of time (usually a couple days) before they are assigned another number. Large organizations (businesses, government) will often have a set of addresses that they always come from and these addresses are static and will not change.

To answer your question, given the above information:

1) IP addresses are not assigned at random. There is usually some sort of logical method for assigning IP addressed to allow for correct and timely routing of traffic across a network and the internet.

2) The distribution of the last octet of an IPv4 address depends upon a number of factors relating to the above information (and the mood of the network administrator, in some cases) and, therefore, not equal. You can not rely on only the last octet to provide 'uniqueness'.

Come on IPv6!

share|improve this answer
Thanks for the detailed answer! –  Adjam May 11 '12 at 22:04

The last octet is likely to be skewed towards the lower end, because networks tend to be numbered from the bottom. A better scheme would be to treat the entire IP address as a 32bit unsigned integer and use the modulo operation to get the remainder when diving by the number of buckets you want. This would assign each subsequent (numerically) address to a different bucket (wrapping around when the last bucket is reached).

For example: = 16909060
16909060 % 3 = 1

So this would put into bucket 1 (where buckets are numbered 0, 1, and 2). would go into bucket 2, and would go into bucket 0.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.