Random redirect not random at all?

Hi i wrote a code to randomly redirect to one of 10 pages, but often same pages is displayed, i got a lot of emails from users that say the same, Is there any mistake?
http://www.tarovartai.lt/konsultacija/kortos/

<?php
/**
 * Created by PhpStorm.
 * User: Azathoth
 * Date: 29. 3. 2016
 * Time: 14:54
 */

$addresses = [
	
	'http://www.tarovartai.lt/konsultacija/korta4',
	'http://www.tarovartai.lt/konsultacija/korta1',
	'http://www.tarovartai.lt/konsultacija/korta3',
	'http://www.tarovartai.lt/konsultacija/korta2',
	'http://www.tarovartai.lt/konsultacija/korta5',
	'http://www.tarovartai.lt/konsultacija/korta6',
	'http://www.tarovartai.lt/konsultacija/korta7',
	'http://www.tarovartai.lt/konsultacija/korta8',

	'http://www.tarovartai.lt/konsultacija/korta9',
	'http://www.tarovartai.lt/konsultacija/korta10'
	
	
	
];

$size = count($addresses);
$randomIndex = rand(0, $size - 1);
$randomUrl = $addresses[$randomIndex];

header('Location: ' . $randomUrl, true, 303);

yes and no. Randomness does not necessarily mean that you cannot have repetition (select the same entry consecutively /1/). To achieve that you would have to explicitly exclude the last chosen entry.

1 - the probability for two consecutive draws is 10%

2 Likes

so if i get 3 in a row its just luck ?

1 Like

Essentially yes. Although I’d rather use mt_rand() than rand().

3 Likes

Can u say why ?

From the manual:

Many random number generators of older libcs have dubious or unknown characteristics and are slow. The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

Edit: If you’re using PHP 7.1 or later, there is no obvious difference between rand() and mt_rand().

Caution: Maths

Because random is not the same as an exhaustive set distribution. What you’re falling for is a combined failure of Sample Size, and an understanding of Individual Tests.

Random generation will result in a uniform distribution over a large sample, because, on average, you will hit each of the random results an equal number of times.

It does NOT however mean that you wont get the same number twice.

Think of it in terms of flipping a coin. If you flip a fair coin unbiased 100,000 times, you will get approximately 50% of each result, give or take some number that will generally fall within a statistical margin. However, if you flip the coin twice and get the same result both times, you’ve got 100% of that result. Is the coin biased? Is it two-sided? No. It’s just that your luck is always 50% to get one or the other, regardless of whether the previous flip was a head or a tail. This last bit is the ‘independent test’ bit. The previous result has no bearing on the next.

So, the chances of you getting one of 10 sites is always* 10%, regardless of what happened before.

By contrast, an Exhaustive Set distribution (also known as a modified Round Robin selector) will give out each address once before refreshing the list once it becomes exhausted.

*: Assuming a properly random selection and distribution algorithm.

3 Likes

On a side note: If you want a classic example of this misunderstanding, ask a class full of people to arrange themselves randomly around a room. What you’ll find most often is that they will spread themselves out as much as possible from one another - which is not random, it’s uniform… Random is where they started.

2 Likes

Like the others have stated, the randomisation is just the same as flipping a coin or rolling the dice, there is always a probability of seeing the same result more than once and that probability is greater as the number of unseen results decreases.

The answer may in knowing exactly the behavior you really want from this.
Perhaps you want to show each of the pages in a shuffled sequence.
Or if there is no real sequence of consecutive pages and the “random” page link may be visited at any time, any number of times, you may require a cookie of some sort to record which of the ten have been seen already. Or maybe an array of the addresses they have yet to see which can be passed to your script.
It all depends upon what you want to happen.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.