Mike521 Posted March 10, 2008 Share Posted March 10, 2008 we recently launched a new version of our internal search results page. to test the difference, I'm dividing visitors into two groups at the time they do a search. They get a cookie telling which group they're in, so they remain in it when they do searches later. For some reason I'm getting about 70% in group A, and 30% in group B. I can't figure out what, if anything, is wrong with my code. Can someone review and tell me if I'm crazy? if ( $_COOKIE["SearchTestGroup"] ) { // they're already in a group $group = $_COOKIE["SearchTestGroup"]; echo "you're already in group $group"; if ( $group == "B" ) { echo "<br>doing group B search"; //doGroupBSearch( $theSearch ); } else { echo "<br>doing group A search"; //doGroupASearch( $theSearch ); } } else { // they're not in a group $group = rand()&1; // 0 = A, 1 = B echo "<br><br>your random number is $group."; echo " A value of 0 puts you in group A. 1 puts you in group B."; if ( $group ) { // group B setcookie( "SearchTestGroup", "B", $cookieExpiration, "/", ".oursite.com" ); // now perform the search echo "<br><br>cookie set as B"; //doGroupBSearch( $theSearch ); } else { setcookie( "SearchTestGroup", "A", $cookieExpiration, "/", ".oursite.com" ); //now perform the search echo "<br><br>cookie set as A"; //doGroupASearch( $theSearch ); } } Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/ Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 How many test cases have you done? Is your sample size large enough for you to even come to that conclusion? You have to also think that, just because you have a 50% chance, that's a long run 50% distribution. Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488747 Share on other sites More sharing options...
discomatt Posted March 10, 2008 Share Posted March 10, 2008 Try $group = mt_rand(0,1); Running that (100x10000 time), I never had anything beyond +/- 5% difference. Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488748 Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 I was also going to suggest using the range parameters. mt_rand() is faster than rand() correct? Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488752 Share on other sites More sharing options...
Mike521 Posted March 10, 2008 Author Share Posted March 10, 2008 I ran a modified version of the script that simply looped 1 million times and tallied up A vs B (using the method I posted earlier). it was almost exactly 50/50, so I'm confident that it's accurate. Regardless, so far we have 2,296 searches. 1,539 were in group A (67%), 757 were in group B (33%). everything seems correct to me but I can't figure out why there's such a big discrepancy Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488754 Share on other sites More sharing options...
Mike521 Posted March 10, 2008 Author Share Posted March 10, 2008 I was also going to suggest using the range parameters. mt_rand() is faster than rand() correct? hmm I'm not sure which is faster myself. I'll switch to it, but I don't think the method is the cause of the problem, does everyone agree? If not, try this yourself: $groupA = 0; $groupB = 0; for ( $i = 0; $i < 1000000; $i++ ){ $random = rand()&1; if ( $random == 1 ) { //echo "<br> it's a 1 (group B)"; $groupB++; } else { if ( $random == 0 ) { //echo "<br> it's a 0 (group A)"; $groupA++; } } } echo "<br>group A: $groupA<br>group B: $groupB"; Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488757 Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 What are your expected results? My output: A: 499939 B: 500061 Looks like it's working fine to me. Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488762 Share on other sites More sharing options...
Mike521 Posted March 10, 2008 Author Share Posted March 10, 2008 yep that's the goal, I want it to be as close to 50/50 as possible but for one reason or another, 67% of our visitors are doing group A's search, rather than 50%. I guess there's a problem outside this PHP code Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488772 Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 I ran your function with rand/mt_rand and with/without ranges. About 50% every time. Also included how long each took in seconds. mt_rand()&1 A: 499430 B: 500570 2 mt_rand(0,1) A: 500009 B: 499991 1 rand()&1 A: 500060 B: 499940 2 rand(0,1) A: 500779 B: 499221 2 Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488774 Share on other sites More sharing options...
Mike521 Posted March 10, 2008 Author Share Posted March 10, 2008 well if nothing else at least I can switch to mt_rand. I'll have to look at javascripts and such to see if there's something redirecting people from page B to page A.. thanks everyone for taking a look at the code for me Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488783 Share on other sites More sharing options...
discomatt Posted March 10, 2008 Share Posted March 10, 2008 From the manual Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function. The mt_rand() function is a drop-in replacement for this. 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. Quote Link to comment https://forums.phpfreaks.com/topic/95478-random-number-to-divide-visitors-into-two-groups-is-my-code-wrong/#findComment-488825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.