Welcome to WebmasterWorld Guest from **54.162.117.84**

I would like to have a fork in my PHP code - the coding will branch with 2 possible options to execute (2 possible web contents to show). Which one is executed will depend on a certain changeble ratio factor. So, for instance, if the ratio is 3:7:

if the PHP code is called 10 times - 3 times option 1 will be run. 7 times option 2 will be run.

My Q is - how can I go about coding this. I really haven't got a clue how to implement this ratio (probability) control.

Just thinking about it quickly i think i found a solution to your problem....

now i haven't tried anything out but im sure it will work.

When the page loads it should retrieve the number of previous visitors to your site from a text file or database. Now depending on what ratio you are using, in your example 3:7, you add the two numbers (i.e. 3+7=10). Now you take the amount of visitors to your site and do something like:

$modulus = $visitors % 10;

if($modulus < 4)

{ do something }

else if($modulus > 3)

{ do something else }

As i said i haven't tried it to see if its flawless but it should work! Good luck...

Actually, as I read it, the OP wants 3/10 and 7/10 (the ratio is "3 to 7" not "3 out of 7"), so you use charlier's logic and dmorison's code and you have:

if (rand(1,10) < 4)

{

//30%

}

else

{

// 70%

}

It's not that simple guys, what you are suggesting can be true only for large number [en.wikipedia.org] of trials (code executions) and even in that case we will never have exactly 3/10 and 7/10.

If a coin is flipped once the probability that it lands heads up is 0.5 or 50%.

But if it is flipped 10 times the probability that it lands heads up **exactly** 5 times is not 0.5 but

using the Binomial Probability Formula [google.com]:

(10!/5!(10-5)!) (1/2)^5) (1/2)^5 = **0.246**

As we increase the number of tosses the relative frequency (heads up/number of tosses) tends to vary less and stay near 0.5, or 50%.

So we need a large number of trials to obtain our theoretical probability (0.5).

If your code can not be executed thousands of times a day you can use the binomial probability formula.

Use as number of trials the times your code will be executed from statistical analysis of the last days or months

As dmorison said

If you're happy for the distribution to average

If it has to be exactly three out of every ten, then he will need some sort of counter system. If he wants a 3 in 10 chance of the page coming up, the pseudo-random number generator should get pretty close to that.

- Register For Free! -
**Become a Pro Member!** - See forum categories - Enter the Forum

- Moderator List | Top Contributors:This Week, This Month, July, June, Archive, Top 100 All Time, Top Voted Members

- Google Updates and SERP Changes - August 2016
- Does Removing Google Analytics Help Your Conversion Rates?
- August 2016 AdSense Earnings and Observations
- Link Maintenance Matters
- I blocked Ad-Blockers.
- SERP With One Organic Result And 7 Adwords Ads
- Adsense 3 ads limit lifted?
- Pinterest Adds Video Advertising
- Google wants to put an end to pop-over ads
- .xyz invasion in a niche we are keeping an eye on

- Google wants to put an end to pop-over ads
- Pinterest Adds Video Advertising
- Link Maintenance Matters
- Chrome 53 Browser Will Block Background Flash Content
- Facebook Finds a Way Round the Ad Blockers
- Google Updates and SERP Changes - August 2016
- World Wide Web is 25 Years Old
- Facebook Changes Newsfeed over Clickbait Headlines
- Firefox Wants to Get Rid of Your Custom 404 Error Pages
- Google Previews Expanded AMP Support in Entire SERPs