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?

  • What makes you think that the last segment is "assigned completely at random"?
    – MrWhite
    May 11, 2012 at 12:02
  • 1
    It's a guess. I am unsure if this guess is correct. That's the content of my question.
    – Adjam
    May 11, 2012 at 16:07
  • Why not just do round robin?
    – Bigbio2002
    May 15, 2012 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, 2012 at 13:01

2 Answers 2


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!


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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