Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

x percent chance of something happening

random percentage

  • Please log in to reply
5 replies to this topic

#1 Alkocen

Alkocen

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 13 September 2012 - 05:42 AM

Hi,

i've been figuring for a while now on how to get some event to trigger on some percentage.

For example: I want that 1% of visitors can see some event.
Only way i've figured out is something like this:

x = 20;
if(x == rand(1, 100)){
  something happens;
}

But i don't trust this approach much, since there is a chance that number 20 will never be selected, statistically improbable but still can happen for thousands of random generations. Even if i have like millions or runs, the percentage for selecting number 20 might not be around 1%, it might be 3% or maybe 0.2%.

So my question is, are there other ways to make this kind of formula happen? Maybe use to redirect traffic or to display some opportunities or whatever.
  • 0

#2 Andre

Andre

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Location:Dakar, Sénégal
  • Programming Language:C, Java, C++, PHP, JavaScript, PL/SQL, Bash, Others
  • Learning:C#, Transact-SQL

Posted 13 September 2012 - 06:41 AM

If you have a database, you can count the number of connections. Everytime a session begins, you update this value. When this value equals 100 you set it to 0 and launch the events.
  • 1

#3 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 13 September 2012 - 06:54 AM

Andre's answer will give you a much more reliable way to filter out exactly 1% of the visitors to your site. The reason is, as you've already said, your target number might not be chosen by the pseudorandom number generator for a very long time. Likewise, it might choose your target number several times in a row. (Rare, but statistically, it will happen given enough trials.)

However, you'll need to be careful when counting the number of connections. Under what conditions should you increment your counter? Every time the webpage is served up? That would mean that users could easily get to the 100th visitor mark simply by refreshing their page. Should you count sessions initiated? Still, a user could clear their browser cache and refresh your page, and this would count as another session, incrementing your counter. Should you count by unique IP addresses? Then everybody behind a NAT firewall will collectively count as one visitor.

Think about it and come up with a solution that you think will be robust enough for what you're wanting to do.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4 programmingservices

programmingservices

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 80 posts
  • Programming Language:C, Java, C++, Objective-C, PHP, JavaScript
  • Learning:PHP

Posted 14 September 2012 - 03:48 AM

Andre's answer will give you a much more reliable way to filter out exactly 1% of the visitors to your site. The reason is, as you've already said, your target number might not be chosen by the pseudorandom number generator for a very long time. Likewise, it might choose your target number several times in a row. (Rare, but statistically, it will happen given enough trials.)

However, you'll need to be careful when counting the number of connections. Under what conditions should you increment your counter? Every time the webpage is served up? That would mean that users could easily get to the 100th visitor mark simply by refreshing their page. Should you count sessions initiated? Still, a user could clear their browser cache and refresh your page, and this would count as another session, incrementing your counter. Should you count by unique IP addresses? Then everybody behind a NAT firewall will collectively count as one visitor.

Think about it and come up with a solution that you think will be robust enough for what you're wanting to do.


Wow Greg You left nothing to comment about. Having said that I'll still keep a watch on the thread to see what's new is coming in the scene?
  • 0

#5 Alkocen

Alkocen

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 14 September 2012 - 05:05 AM

5 minutes after i posted this topic, i had the same idea about using some kind of counter. Either count sessions, db connections or simply page refreshes. But like Greg pointed out, it's not perfect.

Then few minutes ago i had another idea. To still use that rand function but at the same time do a little statistics on how frequently that number 20 pops up. And according to that statistics a script starts nudging it. Like if number 20 pops up too frequently, then the chance of selecting number 20 is lowered and vice versa. Not sure how well it would work though.

But thanks for the tips and thinking along :)
  • 0

#6 BenW

BenW

    CC Devotee

  • Community Manager
  • 976 posts
  • Programming Language:ActionScript
  • Learning:ActionScript

Posted 14 September 2012 - 08:36 AM

You'll have to be careful how you monitor the trigger rate if you're going to use that method. Even if you were triggering it exactly once every 100 pageviews, for the first 99 of those you'd be at a 0% trigger rate, which appears to be too low, but is actually correct.
  • 0

fss_overall.png

 






Also tagged with one or more of these keywords: random, percentage

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download