Random Page in percentage [explanation needed]

LGZACRO

Newbie
Joined
Aug 31, 2013
Messages
17
Reaction score
0
Hello everyone, I am looking how to make that 30 percent of my "pictures.html" visitors would be redirected to page1.html, and 70 percent to page2.html?
I have found these:

Code:
<script type="text/javascript">
(function(n){
 var pages = ['page1.htm', 'page2.htm', 'page3.htm'];
 n = n < 3? 0 : n < 8? 1 : 2;
 window.location.replace(pages[n]);
})(Math.floor(Math.random() * 10));
</script>

but I am not sure I understand it right... can anyone explain this?
 
Hey buddy. Sorry but I dont know javascript as I skipped that for some unknown reason.
But if your site supports PHP I write a litle code for you.
Code:
<?php
$random = rand(1, 100);
if($random > 70){ //If dice hits over 70 (out of 100)do this
echo '<meta http-equiv="refresh" content="0; ,URL=page1.html">'
}else{ //Otherwise, do this
echo '<meta http-equiv="refresh" content="0; ,URL=page2.html">'
}
?>
It should work, if it dosent, let me know as I wrote this just from the air.
 
Last edited:
This won't work because you haven't considered the weightage or frequency of the redirects.

OP: Have a look at this example of banner rotator based on ratio/weight and with slight modification you can achieve what you want.



Hey buddy. Sorry but I dont know javascript as I skipped that for some unknown reason.
But if your site supports PHP I write a litle code for you.
Code:
<?php
$random = rand(1, 100);
if($random > 70){ //If dice hits over 70 (out of 100)do this
echo '<meta http-equiv="refresh" content="0; ,URL=page1.html">'
}else{ //Otherwise, do this
echo '<meta http-equiv="refresh" content="0; ,URL=page2.html">'
}
?>
It should work, if it dosent, let me know as I wrote this just from the air.
 
As OP said 30% of visitors would visit page-1 and 70% would visit page-2.

But in your code, you just check for the value of the random number generated is above 70 and redirecting to page-2. It doesn't mean it will be redirected to page-2 70 out of 100 times. So there's no guarantee of any percentage redirects in this case.



$random = rand(1, 100);



Sorry for my english, could you please explain?
 
Last edited:
^That is a far more complicated solution when there are only two choices and the choices are normalised. The idea posted in the PHP code is perfectly fine. Random numbers will be generated according to Gaussian distribution and so they are all equally likely to occur. Numbers over 70 will in the long run converge to being generated approximately 30% of the time. While it may not guarantee exactly 70% redirection to a one choice and 30% to the other - the code is perfectly fine for when there are only two choices. There is also no guarantee of percentages for the solution you linked. It too relies on random number generation and will also have the deviations from the weights in the same way as the other less complicated solution.
 
As OP said 30% of visitors would visit page-1 and 70% would visit page-2.

But in your code, you just check for the value of the random number generated is above 70 and redirecting to page-2. It doesn't mean it will be redirected to page-2 70 out of 100 times. So there's no guarantee of any percentage redirects in this case.

Yes you're right. There is 70% change for EACH visitor to get redirected to page2.html and not 70% for all traffic in totall.
I only took a look at the javascript in the OP and as I said, I'm no javascripter.
But I got the feeling that the code I posted did the same thing, only diffrence was, it's PHP.

I just tested the PHP code myself and had it load 1Mx10 times, out of 1 million this stats went to page2.html

699839
700245
700437
700188
699829
699859
699695
700392
700166
699835

Out of 10M page loads 69,990923% was redirected to page2.
Leaving 30,01% to page1.

I think it's a fair number.
 
Last edited:
Its best to use "mt_rand" instead "rand". The numbers generated follow uniform distribution, not Gaussian - this is the reason its safe to assume that 30% will visit page 1 and 70% will visit page 2.
 
Back
Top