Random Image Script
- tommya
- Graduate


- Joined: Mar 17, 2004
- Posts: 221
- Loc: United Kingdom
- Status: Offline
Hi,
I am using the following script that I picked up from elsewhere and adapted. However, I want it to display 2 random images and not just the one as this does. I also dont want the 2 random images to ever be the same. Can anybody help
I am using the following script that I picked up from elsewhere and adapted. However, I want it to display 2 random images and not just the one as this does. I also dont want the 2 random images to ever be the same. Can anybody help
Code: [ Select ]
<SCRIPT LANGUAGE="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
}
random_imglink()
//-->
</SCRIPT>
<NOSCRIPT><IMG SRC="images/8.jpg" BORDER=0"></NOSCRIPT>
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
}
random_imglink()
//-->
</SCRIPT>
<NOSCRIPT><IMG SRC="images/8.jpg" BORDER=0"></NOSCRIPT>
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- function random_imglink(){
- var myimages=new Array()
- myimages[1]="/images/1.jpg"
- myimages[2]="/images/2.jpg"
- myimages[3]="/images/3.jpg"
- myimages[4]="/images/4.jpg"
- myimages[5]="/images/5.jpg"
- myimages[6]="/images/6.jpg"
- myimages[7]="/images/7.jpg"
- myimages[8]="/images/8.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- if (ry==0)
- ry=1
- document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
- }
- random_imglink()
- //-->
- </SCRIPT>
- <NOSCRIPT><IMG SRC="images/8.jpg" BORDER=0"></NOSCRIPT>
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
September 15th, 2004, 1:19 pm
- lucassix
- Mastermind


- Joined: Sep 13, 2004
- Posts: 2344
- Loc: Living in a VAN DOWN BY THE RIVER!
- Status: Offline
- neksus
- Mastermind


- Joined: Sep 10, 2004
- Posts: 2194
- Loc: Canada
- Status: Offline
here's one for php:
its quick and dirty but it'll get the job done.
yes i know this could be improved alot better haha but im tired
Code: [ Select ]
<?php
// total number of images
$total = "8";
// file extension
$file_type = ".jpg";
// the first file (they must be in numerical order)
$start = "1";
// randomize
$random = mt_rand($start, $total);
// randomize another variable
$random2 = mt_rand($start + 1, $total);
// try and make them not different if they were
if($random2 == $random){
$random2 = mt_rand($start + 1, $total);
}
// get a filename from random
$image_name = $random . $file_type;
// make another (because you want 2)
$image_name2 = $random . $file_type;
echo "<img src=\"images/$image_name\" width=\"x\" height=\"x\"><img src=\"images/$image_name2\" width=\"x\" height=\"x\">";
?>
// total number of images
$total = "8";
// file extension
$file_type = ".jpg";
// the first file (they must be in numerical order)
$start = "1";
// randomize
$random = mt_rand($start, $total);
// randomize another variable
$random2 = mt_rand($start + 1, $total);
// try and make them not different if they were
if($random2 == $random){
$random2 = mt_rand($start + 1, $total);
}
// get a filename from random
$image_name = $random . $file_type;
// make another (because you want 2)
$image_name2 = $random . $file_type;
echo "<img src=\"images/$image_name\" width=\"x\" height=\"x\"><img src=\"images/$image_name2\" width=\"x\" height=\"x\">";
?>
- <?php
- // total number of images
- $total = "8";
- // file extension
- $file_type = ".jpg";
- // the first file (they must be in numerical order)
- $start = "1";
- // randomize
- $random = mt_rand($start, $total);
- // randomize another variable
- $random2 = mt_rand($start + 1, $total);
- // try and make them not different if they were
- if($random2 == $random){
- $random2 = mt_rand($start + 1, $total);
- }
- // get a filename from random
- $image_name = $random . $file_type;
- // make another (because you want 2)
- $image_name2 = $random . $file_type;
- echo "<img src=\"images/$image_name\" width=\"x\" height=\"x\"><img src=\"images/$image_name2\" width=\"x\" height=\"x\">";
- ?>
its quick and dirty but it'll get the job done.
yes i know this could be improved alot better haha but im tired
- joebert
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13455
- Loc: Florida
- Status: Offline
Code: [ Select ]
<SCRIPT LANGUAGE="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if(ry<1) ry++;
var ry2=Math.floor(Math.random()*myimages.length)
if(ry2<1) ry2++;
if(ry == ry2){ry>1 ? ry2-- : ry2++;}
//alert(ry + "|" + ry2);
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
document.write('<IMG SRC="'+myimages[ry2]+'" BORDER=0><p>')
}
random_imglink()
//-->
</SCRIPT>
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if(ry<1) ry++;
var ry2=Math.floor(Math.random()*myimages.length)
if(ry2<1) ry2++;
if(ry == ry2){ry>1 ? ry2-- : ry2++;}
//alert(ry + "|" + ry2);
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
document.write('<IMG SRC="'+myimages[ry2]+'" BORDER=0><p>')
}
random_imglink()
//-->
</SCRIPT>
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- function random_imglink(){
- var myimages=new Array()
- myimages[1]="/images/1.jpg"
- myimages[2]="/images/2.jpg"
- myimages[3]="/images/3.jpg"
- myimages[4]="/images/4.jpg"
- myimages[5]="/images/5.jpg"
- myimages[6]="/images/6.jpg"
- myimages[7]="/images/7.jpg"
- myimages[8]="/images/8.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- if(ry<1) ry++;
- var ry2=Math.floor(Math.random()*myimages.length)
- if(ry2<1) ry2++;
- if(ry == ry2){ry>1 ? ry2-- : ry2++;}
- //alert(ry + "|" + ry2);
- document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
- document.write('<IMG SRC="'+myimages[ry2]+'" BORDER=0><p>')
- }
- random_imglink()
- //-->
- </SCRIPT>
Easiest way is give the function more to do. Just needs to have the doc..write()s modified & the NOSCRIPT tag updated to use two defaults.
I just duplicated the random assignment, check if the ry numbers are the same, if they are the same check if they are greater than 1, if they are greater than one knock 1 off of ry2 else add 1 to it.
Strong with this one, the sudo is.
- Mas Sehguh
- Mastermind


- Joined: Aug 07, 2004
- Posts: 1853
- Status: Offline
The problem with that is that the randomness is not evenly distributed, and it won't even get a chance of hitting the highest-numbered image (instead, the lowest-numbered image would get hit twice as often).
Use this instead:
Incidentally, you could do this specific problem without an array of URLs, since the file names themselves are numbered. Later, they might not be, though, or they might have leading zeros, so keeping the array is a good idea.
Use this instead:
Code: [ Select ]
<script type="text/javascript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[0] = "/images/0.jpg"
myimages[1] = "/images/1.jpg"
myimages[2] = "/images/2.jpg"
myimages[3] = "/images/3.jpg"
myimages[4] = "/images/4.jpg"
myimages[5] = "/images/5.jpg"
myimages[6] = "/images/6.jpg"
myimages[7] = "/images/7.jpg"
var ry=Math.floor(Math.random()*myimages.length)
// if(ry<1) ry++; not needed because array is zero-based.
var rya=Math.floor(Math.random()*(myimages.length - 1))
// now this SECOND piece of code will pick a random number
// of length myimages.length - 1.
if(rya >= ry) rya++; //now increment rya if it needs it.
// even if rya is greater than ry, it still needs incrementation
// because rya needs to be evenly distributed across the
// myimages.length - 1 remaining options.
// document.write(ry + " | " + rya);
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
document.write('<IMG SRC="'+myimages[rya]+'" BORDER=0><p>')
}
random_imglink()
// -->
</script>
<!--
function random_imglink(){
var myimages=new Array()
myimages[0] = "/images/0.jpg"
myimages[1] = "/images/1.jpg"
myimages[2] = "/images/2.jpg"
myimages[3] = "/images/3.jpg"
myimages[4] = "/images/4.jpg"
myimages[5] = "/images/5.jpg"
myimages[6] = "/images/6.jpg"
myimages[7] = "/images/7.jpg"
var ry=Math.floor(Math.random()*myimages.length)
// if(ry<1) ry++; not needed because array is zero-based.
var rya=Math.floor(Math.random()*(myimages.length - 1))
// now this SECOND piece of code will pick a random number
// of length myimages.length - 1.
if(rya >= ry) rya++; //now increment rya if it needs it.
// even if rya is greater than ry, it still needs incrementation
// because rya needs to be evenly distributed across the
// myimages.length - 1 remaining options.
// document.write(ry + " | " + rya);
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
document.write('<IMG SRC="'+myimages[rya]+'" BORDER=0><p>')
}
random_imglink()
// -->
</script>
- <script type="text/javascript">
- <!--
- function random_imglink(){
- var myimages=new Array()
- myimages[0] = "/images/0.jpg"
- myimages[1] = "/images/1.jpg"
- myimages[2] = "/images/2.jpg"
- myimages[3] = "/images/3.jpg"
- myimages[4] = "/images/4.jpg"
- myimages[5] = "/images/5.jpg"
- myimages[6] = "/images/6.jpg"
- myimages[7] = "/images/7.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- // if(ry<1) ry++; not needed because array is zero-based.
- var rya=Math.floor(Math.random()*(myimages.length - 1))
- // now this SECOND piece of code will pick a random number
- // of length myimages.length - 1.
- if(rya >= ry) rya++; //now increment rya if it needs it.
- // even if rya is greater than ry, it still needs incrementation
- // because rya needs to be evenly distributed across the
- // myimages.length - 1 remaining options.
- // document.write(ry + " | " + rya);
- document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
- document.write('<IMG SRC="'+myimages[rya]+'" BORDER=0><p>')
- }
- random_imglink()
- // -->
- </script>
Incidentally, you could do this specific problem without an array of URLs, since the file names themselves are numbered. Later, they might not be, though, or they might have leading zeros, so keeping the array is a good idea.
- Scorpius
- Proficient


- Joined: Mar 20, 2004
- Posts: 401
- Loc: Scorpion Hole
- Status: Offline
Try this:
I think this would probably be best and more random than the other ways, although you have two arrays, you will always have different images.
Code: [ Select ]
<script type="text/javascript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[0] = "/images/0.jpg"
myimages[1] = "/images/1.jpg"
myimages[2] = "/images/2.jpg"
myimages[3] = "/images/3.jpg"
var myimagesa=new Array()
myimagesa[0] = "/images/4.jpg"
myimagesa[1] = "/images/5.jpg"
myimagesa[2] = "/images/6.jpg"
myimagesa[3] = "/images/7.jpg"
var ry=Math.floor(Math.random()*myimages.length)
var rya=Math.floor(Math.random()*(myimagesa.length))
// document.write(ry + " | " + rya);
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
document.write('<IMG SRC="'+myimagesa[rya]+'" BORDER=0><p>')
}
random_imglink()
// -->
</script>
<!--
function random_imglink(){
var myimages=new Array()
myimages[0] = "/images/0.jpg"
myimages[1] = "/images/1.jpg"
myimages[2] = "/images/2.jpg"
myimages[3] = "/images/3.jpg"
var myimagesa=new Array()
myimagesa[0] = "/images/4.jpg"
myimagesa[1] = "/images/5.jpg"
myimagesa[2] = "/images/6.jpg"
myimagesa[3] = "/images/7.jpg"
var ry=Math.floor(Math.random()*myimages.length)
var rya=Math.floor(Math.random()*(myimagesa.length))
// document.write(ry + " | " + rya);
document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
document.write('<IMG SRC="'+myimagesa[rya]+'" BORDER=0><p>')
}
random_imglink()
// -->
</script>
- <script type="text/javascript">
- <!--
- function random_imglink(){
- var myimages=new Array()
- myimages[0] = "/images/0.jpg"
- myimages[1] = "/images/1.jpg"
- myimages[2] = "/images/2.jpg"
- myimages[3] = "/images/3.jpg"
- var myimagesa=new Array()
- myimagesa[0] = "/images/4.jpg"
- myimagesa[1] = "/images/5.jpg"
- myimagesa[2] = "/images/6.jpg"
- myimagesa[3] = "/images/7.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- var rya=Math.floor(Math.random()*(myimagesa.length))
- // document.write(ry + " | " + rya);
- document.write('<IMG SRC="'+myimages[ry]+'" BORDER=0><p>')
- document.write('<IMG SRC="'+myimagesa[rya]+'" BORDER=0><p>')
- }
- random_imglink()
- // -->
- </script>
I think this would probably be best and more random than the other ways, although you have two arrays, you will always have different images.
- Mas Sehguh
- Mastermind


- Joined: Aug 07, 2004
- Posts: 1853
- Status: Offline
Actually, it is less random. There are 16 possible combinations of images in your example, 28 in my previous example (and in mine, the order is random too), and both avoid having the same image appear twice.
Why prevent 1.jpg and 2.jpg from being shown at the same time?
Why prevent 1.jpg and 2.jpg from being shown at the same time?
- joebert
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13455
- Loc: Florida
- Status: Offline
Quote:
The problem with that is that the randomness is not evenly distributed, and it won't even get a chance of hitting the highest-numbered image (instead, the lowest-numbered image would get hit twice as often).
Sam there's no difference between our functions results, Both are just as random & they both take a maximum of 140 milliseconds to run 500 times on a 500mhz cpu....
Code: [ Select ]
<html>
<head>
<style type="text/css">
body{background:#123456; color:#ffffff;}
</style>
</head>
<body>
<div style="position:absolute; left:100px;">
<script type="text/javascript">
<!--
function random_imglin(){
var myimages=new Array()
myimages[0] = "/images/0.jpg"
myimages[1] = "/images/1.jpg"
myimages[2] = "/images/2.jpg"
myimages[3] = "/images/3.jpg"
myimages[4] = "/images/4.jpg"
myimages[5] = "/images/5.jpg"
myimages[6] = "/images/6.jpg"
myimages[7] = "/images/7.jpg"
var ry=Math.floor(Math.random()*myimages.length)
var rya=Math.floor(Math.random()*(myimages.length - 1))
if(rya >= ry) rya++; //now increment rya if it needs it.
document.write(ry + "|" + rya + "<br>");
}
time = new Date().getMilliseconds();
for(var i=0; i<500; i++){random_imglin();}
document.write(new Date().getMilliseconds() - time);
// -->
</script>
</div>
<div style:position:absolute; left:0px;">
<SCRIPT LANGUAGE="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if(ry<1) ry++;
var ry2=Math.floor(Math.random()*myimages.length)
if(ry2<1) ry2++;
if(ry == ry2){ry>1 ? ry2-- : ry2++;}
document.write(ry + "|" + ry2 + "<br>");
}
time = new Date().getMilliseconds();
for(var i=0; i<500; i++){random_imglink();}
document.write(new Date().getMilliseconds() - time);
//-->
</SCRIPT>
</div>
</body>
</html>
<head>
<style type="text/css">
body{background:#123456; color:#ffffff;}
</style>
</head>
<body>
<div style="position:absolute; left:100px;">
<script type="text/javascript">
<!--
function random_imglin(){
var myimages=new Array()
myimages[0] = "/images/0.jpg"
myimages[1] = "/images/1.jpg"
myimages[2] = "/images/2.jpg"
myimages[3] = "/images/3.jpg"
myimages[4] = "/images/4.jpg"
myimages[5] = "/images/5.jpg"
myimages[6] = "/images/6.jpg"
myimages[7] = "/images/7.jpg"
var ry=Math.floor(Math.random()*myimages.length)
var rya=Math.floor(Math.random()*(myimages.length - 1))
if(rya >= ry) rya++; //now increment rya if it needs it.
document.write(ry + "|" + rya + "<br>");
}
time = new Date().getMilliseconds();
for(var i=0; i<500; i++){random_imglin();}
document.write(new Date().getMilliseconds() - time);
// -->
</script>
</div>
<div style:position:absolute; left:0px;">
<SCRIPT LANGUAGE="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if(ry<1) ry++;
var ry2=Math.floor(Math.random()*myimages.length)
if(ry2<1) ry2++;
if(ry == ry2){ry>1 ? ry2-- : ry2++;}
document.write(ry + "|" + ry2 + "<br>");
}
time = new Date().getMilliseconds();
for(var i=0; i<500; i++){random_imglink();}
document.write(new Date().getMilliseconds() - time);
//-->
</SCRIPT>
</div>
</body>
</html>
- <html>
- <head>
- <style type="text/css">
- body{background:#123456; color:#ffffff;}
- </style>
- </head>
- <body>
- <div style="position:absolute; left:100px;">
- <script type="text/javascript">
- <!--
- function random_imglin(){
- var myimages=new Array()
- myimages[0] = "/images/0.jpg"
- myimages[1] = "/images/1.jpg"
- myimages[2] = "/images/2.jpg"
- myimages[3] = "/images/3.jpg"
- myimages[4] = "/images/4.jpg"
- myimages[5] = "/images/5.jpg"
- myimages[6] = "/images/6.jpg"
- myimages[7] = "/images/7.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- var rya=Math.floor(Math.random()*(myimages.length - 1))
- if(rya >= ry) rya++; //now increment rya if it needs it.
- document.write(ry + "|" + rya + "<br>");
- }
- time = new Date().getMilliseconds();
- for(var i=0; i<500; i++){random_imglin();}
- document.write(new Date().getMilliseconds() - time);
- // -->
- </script>
- </div>
- <div style:position:absolute; left:0px;">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- function random_imglink(){
- var myimages=new Array()
- myimages[1]="/images/1.jpg"
- myimages[2]="/images/2.jpg"
- myimages[3]="/images/3.jpg"
- myimages[4]="/images/4.jpg"
- myimages[5]="/images/5.jpg"
- myimages[6]="/images/6.jpg"
- myimages[7]="/images/7.jpg"
- myimages[8]="/images/8.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- if(ry<1) ry++;
- var ry2=Math.floor(Math.random()*myimages.length)
- if(ry2<1) ry2++;
- if(ry == ry2){ry>1 ? ry2-- : ry2++;}
- document.write(ry + "|" + ry2 + "<br>");
- }
- time = new Date().getMilliseconds();
- for(var i=0; i<500; i++){random_imglink();}
- document.write(new Date().getMilliseconds() - time);
- //-->
- </SCRIPT>
- </div>
- </body>
- </html>
Perhaps you should actually test somthing before "noticing a problem" with it
Strong with this one, the sudo is.
- Mas Sehguh
- Mastermind


- Joined: Aug 07, 2004
- Posts: 1853
- Status: Offline
Actually maybe it hits the last image...
However, yours picks myimages[1] twice as often as it picks the rest of the images, because that image gets matched by random numbers in the interval [0,2), which has a width of two, whereas the others have ranges of [2,3), [3,4), which have widths of one. They are not "just as random." If you want to test it, fine:
The results of one run:
Notice how image one gets chosen about 2/9 of the time, the rest getting chosen 1/9 of the time, instead of each being 1/8.
Perhaps you should actually test something before declaring them "just as random"
However, yours picks myimages[1] twice as often as it picks the rest of the images, because that image gets matched by random numbers in the interval [0,2), which has a width of two, whereas the others have ranges of [2,3), [3,4), which have widths of one. They are not "just as random." If you want to test it, fine:
Code: [ Select ]
<SCRIPT LANGUAGE="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if(ry<1) ry++;
var ry2=Math.floor(Math.random()*myimages.length)
if(ry2<1) ry2++;
if(ry == ry2){ry>1 ? ry2-- : ry2++;}
document.write(ry + "|" + ry2 + "<br>");
return ry;
}
var indexsum = new Array()
for(var i = 1; i < 9; i++)
{
indexsum[i] = 0;
}
for(var i=0; i<10000; i++)
{
indexsum[random_imglink()]++;
}
for(var i = 1; i < 9; i++)
{
document.write("Index " + i + ": " + indexsum[i] + "<br>");
}
//-->
</SCRIPT>
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="/images/1.jpg"
myimages[2]="/images/2.jpg"
myimages[3]="/images/3.jpg"
myimages[4]="/images/4.jpg"
myimages[5]="/images/5.jpg"
myimages[6]="/images/6.jpg"
myimages[7]="/images/7.jpg"
myimages[8]="/images/8.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if(ry<1) ry++;
var ry2=Math.floor(Math.random()*myimages.length)
if(ry2<1) ry2++;
if(ry == ry2){ry>1 ? ry2-- : ry2++;}
document.write(ry + "|" + ry2 + "<br>");
return ry;
}
var indexsum = new Array()
for(var i = 1; i < 9; i++)
{
indexsum[i] = 0;
}
for(var i=0; i<10000; i++)
{
indexsum[random_imglink()]++;
}
for(var i = 1; i < 9; i++)
{
document.write("Index " + i + ": " + indexsum[i] + "<br>");
}
//-->
</SCRIPT>
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- function random_imglink(){
- var myimages=new Array()
- myimages[1]="/images/1.jpg"
- myimages[2]="/images/2.jpg"
- myimages[3]="/images/3.jpg"
- myimages[4]="/images/4.jpg"
- myimages[5]="/images/5.jpg"
- myimages[6]="/images/6.jpg"
- myimages[7]="/images/7.jpg"
- myimages[8]="/images/8.jpg"
- var ry=Math.floor(Math.random()*myimages.length)
- if(ry<1) ry++;
- var ry2=Math.floor(Math.random()*myimages.length)
- if(ry2<1) ry2++;
- if(ry == ry2){ry>1 ? ry2-- : ry2++;}
- document.write(ry + "|" + ry2 + "<br>");
- return ry;
- }
- var indexsum = new Array()
- for(var i = 1; i < 9; i++)
- {
- indexsum[i] = 0;
- }
- for(var i=0; i<10000; i++)
- {
- indexsum[random_imglink()]++;
- }
- for(var i = 1; i < 9; i++)
- {
- document.write("Index " + i + ": " + indexsum[i] + "<br>");
- }
- //-->
- </SCRIPT>
The results of one run:
Code: [ Select ]
Index 1: 2259
Index 2: 1095
Index 3: 1093
Index 4: 1059
Index 5: 1085
Index 6: 1134
Index 7: 1090
Index 8: 1185
Index 2: 1095
Index 3: 1093
Index 4: 1059
Index 5: 1085
Index 6: 1134
Index 7: 1090
Index 8: 1185
- Index 1: 2259
- Index 2: 1095
- Index 3: 1093
- Index 4: 1059
- Index 5: 1085
- Index 6: 1134
- Index 7: 1090
- Index 8: 1185
Notice how image one gets chosen about 2/9 of the time, the rest getting chosen 1/9 of the time, instead of each being 1/8.
Perhaps you should actually test something before declaring them "just as random"
- joebert
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13455
- Loc: Florida
- Status: Offline
- DY123
- Born


- Joined: Jan 04, 2011
- Posts: 1
- Status: Offline
Hi,
I came across an old duscussion about multiple random images.
programming-forum/random-image-script-t31615.html
In it you wrote some code to display 2 images. ry and rya.
I would like to add a third ryb.
I've added to your code and it all seems to work fine, but every so often I get a repeat of the images. ry and ryb will be the same.
I think where I need to fix this is in this line:
if(rya >= ry) rya++;
I've done this which I know is wrong:
if(ryb >= ry || ryb >= rya) ryb++;
Have any suggestions?
Thanks
I came across an old duscussion about multiple random images.
programming-forum/random-image-script-t31615.html
In it you wrote some code to display 2 images. ry and rya.
I would like to add a third ryb.
I've added to your code and it all seems to work fine, but every so often I get a repeat of the images. ry and ryb will be the same.
I think where I need to fix this is in this line:
if(rya >= ry) rya++;
I've done this which I know is wrong:
if(ryb >= ry || ryb >= rya) ryb++;
Have any suggestions?
Thanks
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 11 posts
- Users browsing this forum: No registered users and 127 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
