/    Sign up×
Community /Pin to ProfileBookmark

A bit of aid with Actions based on a percentage chance

Heya everyone…

This may be very easy, but I simply cannot think at it. Heh..

I have 5 functions. I would like each function to have a “percentage” of being triggered every time the page loads, but only one function will be triggered per page.

For example, I have 5 functions each with the alert (‘function 1’), (‘function 2’), …. (‘function 5’);

Function 1 has a 30% chance of being triggered when the page loads
Function 2 has a 20% chance of being triggered
Function 3 has a 80% chance of being triggered
Function 4 only 10% and
Function 5 has a 50% chance of being triggered when the page loads

And at the end of the day, I want only the first function in that order that managed to get triggered to actually… trigger, the rest of them not to, even if they have the chance too.

Can someone give me some help on this? I don’t even know where to start..

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@nshiellSep 13.2007 — If i understand you give this a go: -

[code=php]
function innit()
{
if (shouldGo(30))
{
func1()
return true
}


if (shouldGo(20))
{
func2()
return true
}

if (shouldGo(80))
{
func3()
return true
}

}

function shouldGo(percentage)
{
rand = Math.random() * 100
return rand <= percentage
}
[/code]
Copy linkTweet thisAlerts:
@David_HarrisonSep 13.2007 — If only one function should be triggered, then why do the chances of being triggered not add up to 100%?window.onload = function(){

var chance = Math.random();

if( chance &lt; 0.2 ){
function1(); // 0.2 - 0 = 20% chance
}
else if( chance &lt; 0.5 ){
function2(); // 0.5 - 0.2 = 30% chance
}
else if( chance &lt; 0.65 ){
function3(); // 0.65 - 0.5 = 15% chance
}
else if( chance &lt; 0.83 ){
function4(); // 0.83 - 0.65 = 18% chance
}
else{
function5(); // 1 - 0.83 = 17% chance
}

}
In this code, only one function will be run, and the odds add up to 100%.
Copy linkTweet thisAlerts:
@nshiellSep 13.2007 — The widget i wrote for DeltaOne generates a new random number each time.

Thats whaty he asked for
Copy linkTweet thisAlerts:
@David_HarrisonSep 13.2007 — I'm merely offering an alternative, since he said that he only wanted one function to run, it's a better solution to only generate one random number and decide which function runs based on that.

Consider the example he gave, the functions are in order with odds 0.3, 0.2, 0.8, 0.1 and 0.5. But the odds of running the fifth function are not 50%, because it is much more likely that another function will be run before it. The odds are actually, 0.7 * 0.8 * 0.2 * 0.9 * 0.5 = 0.0504 which is only 5.04 %.

Additionally, with a script such as that, there is also the possibility that no function will be run at all, (the odds for which also happen to be 5.04%, since the odds for the last function are 50/50).

Just because that is what DeltaOne asked for, does not mean that is what he wants, there may be consequences (aka bugs) which he has not considered.

Edit: Just to be clear, I'd like to show that the odds for the functions running are not what they appear to be, these are the true odds:

function1 - 30% chance of running

function2 - 14% chance of running (not 20%)

function3 - 44.8% chance of running (not 80%)

function4 - 1.12% chance of running (not 10%)

function5 - 5.04% chance of running (not 50%)

5.04% chance of no function running (presumably this is undesirable)
Copy linkTweet thisAlerts:
@David_HarrisonSep 13.2007 — OK, I've written another script that I guess you could say has the best of both worlds. You can add in your chances as a number, however, the number you enter is treated as a ratio, not a percentage, which means that the numbers don't have to add up to 100. A function will always be run.

I used the values you gave in your example, but since they a re not percentages in this script, the odds are still not what they appear, however that's less important now. The true odds are as follows:

function1 : 10.526 %

function2 : 15.789 %

function3 : 42.105 %

function4 : 5.263 %

function5 : 26.316 %

[upl-file uuid=4ae3617b-9b91-4cec-9029-1ed69178724a size=1kB]function loader.txt[/upl-file]
Copy linkTweet thisAlerts:
@DeltaOneauthorSep 13.2007 — I actually did play a bit with what you guys said, and I have to admit, I never did thought at the "real" chances of something happening.

Anyway, I think that it is best to use David Harrison's idea in his first post, since I've worked with that a bit in my tests. However, I have yet another problem, less complicated than this (hopefully)..


How more exactly can I make another function that grabs a random number between 20 and 60? Any help?

I hate these Math.() things.. Thanks...
Copy linkTweet thisAlerts:
@David_HarrisonSep 13.2007 — Well, Math.random() generates a random number like so:

0 <= num < 1

So to get a number in a different range, you have to do a couple of things, first up, determine the size of the range, in your case it's 40, since 60 - 20 = 40. So you have to multiply the generated value by the size of your range. Then since you don't want a range of 0 to 40, you can shift the range up by adding 20.var blah = (Math.random() * 40) + 20; // Generates a random number such that: 20 &lt;= var &lt; 60
Copy linkTweet thisAlerts:
@DeltaOneauthorSep 13.2007 — Thanks!
×

Success!

Help @DeltaOne spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...