Results 1 to 10 of 10

Thread: Probablity of a Image showing...

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default Probablity of a Image showing...

    Ok, I think that Javascript can do this. Because it involves some basic math. But I don't know any Javascript and my site is going to launch on September 8th, 2007. I still have a lot to go on the site.

    I was wondering if there is a script that can does a sort of random pick. But it has a probability of how many times it will pop up. I got an example below:
    I have 3 Advertisements. I want 1 of them to show up more than the rest. So the probablilties would be:
    1st Banner 70% of the time it is the chosen one
    2nd Banner 5% of the time it is the chosen one
    3rd Banner 25% of the time it is the chosen one

    it would sort be like a ad rotator, but I looked at the ad rotators on dynamic drive, there not that good.

    Thanks to all!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Use random numbers and ifs to achieve this.

    I think it's something like this:

    Code:
    var imgsrc;
    var n = math.rand(0,100);
    if (n<=70) { imgsrc = '1.jpg'; }
    else if (n<=95) { imgsrc = '2.jpg'; }
    else { imgsrc = '3.jpg'; }
    You could simplify the math a bit, perhaps, using smaller numbers, like 0,10 then if <= 7, and then 0,6 then if <= 5, etc.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Ok, i get the first 3 lines, but then it's confusing its the
    Code:
    else if (n<=95)
    which doesn't make any sense to me... but i'll try it anyway...

    Do you think there is a way to apply links to the images since they are "Advertisements"... lol...

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Close but no cigar:
    Code:
    var img = document.images['image_name_or_id'],
      n = Math.random() * 100;
    if(n < 70)
      img.src = "1.jpg";
    else if(n < 95)
      img.src = "2.jpg";
    else
      img.src = "3.jpg";
    It could perhaps be neatened:
    Code:
    function getFromRange(o, n) {
      var last, x;
      for(var x in o)
        if(o.hasOwnProperty(x) && !isNaN(+x)) {
          if(+x < n)
            return o[last] || o.default;
          last = x;
        }
      return o.default;
    }
    
    document.images['image_name_or_id'] = getFromRange({
      '70' : '1.jpg',
      '95' : '2.jpg',
      'default' : '3.jpg'
    }, Math.random() * 100);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Unfortunitly it still doesn't work...

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The latter code may have a bug, but the former is too simple. If you've tried that, I suspect you're implementing it incorrectly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This works (as does Twey's "simple" version):

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <img id="bob" src="photo1.jpg">
    <script type="text/javascript">
    function setPercent(po){
    var x,t=i=0,a=[],r=Math.floor(Math.random()*100);
    for (x in po)
    a[a.length]=x-0;
    if(eval(a.join('+'))!=100){
    alert('Must Total 100%!');
    return po[x];
    }
    a.sort(function(a,b){return a-b;});
    for (i ; i < a.length; i++)
    if(r<(t=t+a[i]))
    return po[a[i]];
    }
    document.images['bob'].src=setPercent({25:'photo2.jpg',70:'photo1.jpg',5:'photo3.jpg'});
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, well I was assuming you would set the image later, but that would be more efficient.

    Note that you should either use <= or < (n+1) to make it 70&#37; and 25%, not 69% and 24%.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by djr33 View Post
    Note that you should either use <= or < (n+1) to make it 70% and 25%, not 69% and 24%.
    I'm not sure that's true, in fact I'm pretty sure that it isn't. The random number generated in my working example would be from 0 to 99 inclusive. You've got to remember that, in javascript, things often start with a 0, so math operations are usually offset by minus 1, but usually still work out like the ones we learned in elementary school that started with a 1.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, I wasn't considering that. That's fine, then.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •