View Full Version : Probablity of a Image showing...

08-23-2007, 05:29 PM
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!

08-23-2007, 06:03 PM
Use random numbers and ifs to achieve this.

I think it's something like this:

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.

08-23-2007, 06:24 PM
Ok, i get the first 3 lines, but then it's confusing its the
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...

08-23-2007, 07:06 PM
Close but no cigar:
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";
img.src = "3.jpg";It could perhaps be neatened:
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);

08-23-2007, 07:22 PM
Unfortunitly it still doesn't work...

08-23-2007, 07:31 PM
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.

08-23-2007, 10:06 PM
This works (as does Twey's "simple" version):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<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)
alert('Must Total 100%!');
return po[x];
a.sort(function(a,b){return a-b;});
for (i ; i < a.length; i++)
return po[a[i]];

08-23-2007, 10:39 PM
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%.

08-23-2007, 10:56 PM
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.

08-24-2007, 12:41 AM
Oh, I wasn't considering that. That's fine, then.