I am trying to pick 3 random numbers from an array length. (I have 5 images that I need to show 3 of each time a page loads - but none can be the same.)
So I need to pick 3 random images from a list of 5 and ther ecan be no duplicates - like I have already done in this post
Anyway I grabbed a script from somewhere I can't remember. and modified it slightly - but I keep getting "undefined" returned as a result.
If you try this code you will see what I mean...
//creates new array filled w/ images and new array filled w/ URL's
images = new Array("agility","strength","balance","power","speed")
//So you can add and subtract images as you choose w/o worry
imgCt = images.length
<!-- Begin
num= new Array(imgCt);
for (i in images) {
num=Math.round(imgCt*Math.random()+1);
if (num[1]==num[0]) {
while (num[1]==num[0]) {
num[1]=Math.round(imgCt*Math.random()+1);
}
}
if (num[2]==num[1] || num[2]==num[0]) {
while (num[2]==num[1] || num[2]==num[0]) {
num[2]=Math.round(imgCt*Math.random()+1);
}
}
if (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
while (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
num[3]=Math.round(imgCt*Math.random()+1);
}
}
document.write(images[num],"-",i,",");
//This is here for testing purposes only
}
Then I want to put this in the page -
document.write ("<a href='programs/'" + eval(images[num[0]]) + "><img src='img/" + eval(images[num[0]]) + ".jpg' width=750 height='122' border='0' alt=''></a><br>");
Can anyone help me find the solution here? ( I will be away fro a few days at this point but at least I have it out there for you to see.
Thanks so much,
Chris
Chris Luksha
Echo Web Services
Making Your Website Resound
So I need to pick 3 random images from a list of 5 and ther ecan be no duplicates - like I have already done in this post
Anyway I grabbed a script from somewhere I can't remember. and modified it slightly - but I keep getting "undefined" returned as a result.
If you try this code you will see what I mean...
//creates new array filled w/ images and new array filled w/ URL's
images = new Array("agility","strength","balance","power","speed")
//So you can add and subtract images as you choose w/o worry
imgCt = images.length
<!-- Begin
num= new Array(imgCt);
for (i in images) {
num=Math.round(imgCt*Math.random()+1);
if (num[1]==num[0]) {
while (num[1]==num[0]) {
num[1]=Math.round(imgCt*Math.random()+1);
}
}
if (num[2]==num[1] || num[2]==num[0]) {
while (num[2]==num[1] || num[2]==num[0]) {
num[2]=Math.round(imgCt*Math.random()+1);
}
}
if (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
while (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
num[3]=Math.round(imgCt*Math.random()+1);
}
}
document.write(images[num],"-",i,",");
//This is here for testing purposes only
}
Then I want to put this in the page -
document.write ("<a href='programs/'" + eval(images[num[0]]) + "><img src='img/" + eval(images[num[0]]) + ".jpg' width=750 height='122' border='0' alt=''></a><br>");
Can anyone help me find the solution here? ( I will be away fro a few days at this point but at least I have it out there for you to see.
Thanks so much,
Chris
Chris Luksha
Echo Web Services
Making Your Website Resound