SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict picasso-trigger's Avatar
    Join Date
    Dec 2003
    Location
    somewhere out there
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do I make randomized content less random with %'s?

    i am looking to to write script that will pull variables based on percentages.

    for example ...

    30% of the time, var1 = 1;
    20% of the time, var1 = 2;
    50% of the time, var1 = 3;

    i'm really not sure how to implement this.

    any ideas?
    _________________________

  2. #2
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe something like this:

    PHP Code:
    function percentRandom()
    {
         
    $rand=rand(0,100);

         if(
    $rand<=20//0-20
         
    {
              return 
    2;
         }

         if(
    $rand<=50//21-50
         
    {
              return 
    1;
         }

         return 
    3//51-100

    }

    $var=percentRandom(); 

  3. #3
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $n rand(1100);
    if (
    $n <= 30)
      
    $var1 1;
    elseif (
    30 $n && $n <= 50)
      
    $var1 2;
    else
      
    $var1 3

  4. #4
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    youngtwig was faster

    although his script has a minor mistake:
    since rand($min, $max) returns a number between $min and $max inclusive, rand(0, 100) actually returns 101 possible values, meaning that number 2 will occur more often (in 21 of 101 tries) than in 20% of the time. But since the difference is really small, this could be called hairsplitting

  5. #5
    SitePoint Addict picasso-trigger's Avatar
    Join Date
    Dec 2003
    Location
    somewhere out there
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow guys ... you rock ... i'll be trying this shortly
    almost at that part
    _________________________

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    theres always this way too

    PHP Code:

    $num 
    mtrand(0100);

    switch (
    $num <= 20 | ($num 20 && $num <= 50) | ($num 50 && $num <= 100)) {
         case (
    true false false):
              
    //20% of the time, $var1 is 1
              
    $var1 1;
              break;
         case (
    false true false):
              
    //30% of the time, $var1 is 2
              
    $var1 2;
              break;
         case (
    false false true):
              
    //50% of the time, $var1 is 3
              
    $var1 3;
              break;


  7. #7
    SitePoint Addict picasso-trigger's Avatar
    Join Date
    Dec 2003
    Location
    somewhere out there
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    both repped.

    so simple!
    _________________________

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    picasso-trigger, glad we helped

    altecex, I've never seen such switch-statement syntax ... Even though I understand it, it seems too confusing to me.

    Also, you have made the same mistake as youngtwig - you pick your number between 0 and 100 (inclusive) to get 100 possible return values, but you get 101 and your distribution of resulting numbers is slighly off balance :P

    /slap me
    don't I have anything better to do then to poke people and split hair?

    Best regards

  9. #9
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    this could be called hairsplitting
    As long as it's not Hehir-splitting.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •