Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomized order of image links? Help from the pros please. 2

Status
Not open for further replies.
Jul 13, 2001
180
US
Hello,

I have 20 different people with their image links on my site. I would like to have them appear randomly in order rather than alphabetically and manually added since I must add more image links.

How can I display these image links in random order making certainb that they also don't repeat.

Thank you,

Manny
 
<script>

picArr = new Array(&quot;pic1.jpg&quot;,&quot;pic2.jpg&quot;,&quot;pic3.jpg&quot;>
numPics = picArr.length

function setPic(){
randomNum = Math.floor(Math.random() * numPics)
document.myPicture.src = picArr[randomNum]
}
</script>

<body onLoad=&quot;setPic()&quot;>
<img src=&quot;blank.jpg&quot; name=&quot;myPicture&quot;>


You may want to add cookies to track the history to stop repeating...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
damn - gave you a javascript answer - sorry... Do I need to change it for you?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Hello and thanks. To be honest I am familiar with VBscript and not Javascript despite some of their similarities.
BTW, would the above display all the pics in random order WITHOUT a repeat? i.e.
Pics 1 2 3 4 5

4
2
1
5
3

Thanks again. ;)
 
In order to make sure not to repeat (I assume you want to load only one picture when the page loads), you need to keep a record of the pics that have already been displayed. Can you elaborate on what your trying to do? Are you using any server-side code?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Hello there. :)

To be completely concise I have 20 rollover images that are links to different sponsors of an event. To avoid a sense of favoristism and the usual alphabetical order ( which would be easier BUT I have to keep changing the order whenever a new sponsor is added). A random display in order of them would be great. For 2 reasons. 1) They can't haggle who's first and 2) I can just &quot;drop&quot; in new sponsors as they come.

SO if there are 20 sponsors, I'd like ALL of them to show , but in random order.

In terms of not repeating, I just want them to show all show ALL at once in a table or cloumn BUT NOT duplicated.

i.e.:
pic 1 pic pic 3

Randomized.

pic 3 pic 1 pic 2

and not

pic 2 pic 1 pic 2


That possible? :)

Thanks again! :)
 
<script language=vbs>
dim myPics
sub setPics()
myPics = array (&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;)
outStr = &quot;&quot;
randomize()
do while uBound(myPics) > 0
ranNum = fix(Rnd() * (uBound(myPics)+1))
outStr = outStr & &quot;<img src='&quot; & myPics(ranNum) & &quot;' title='&quot; & myPics(ranNum) & &quot;'>&quot;
resetArray(ranNum)
loop
'add remaining picture
outStr = outStr & &quot;<img src='&quot; & myPics(0) & &quot;' title='&quot; & myPics(0) & &quot;'>&quot;
document.getElementById(&quot;picPlace&quot;).innerHTML = outStr
end sub

sub resetArray(usedVal)
dim tempArr(), arrCount
arrCount = 0
for x = 0 to uBound(myPics)
if x <> usedVal then
redim preserve tempArr(arrCount)
tempArr(arrCount) = myPics(x)
arrCount = arrCount + 1
end if
next
redim myPics(uBound(tempArr))
for x = 0 to uBound(tempArr)
myPics(x) = tempArr(x)
next
end sub
</script>
<body onLoad=&quot;setPics()&quot;>
<div id=&quot;picPlace&quot;></div>


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Thank you! However it works on IE and comesup blank in Netscape and Opera.

BTwm is there a difference in using
<script language=vbs> as opposed to
<%@LANGUAGE=&quot;VBSCRIPT&quot;%>

Thanks again! :)
 
Here you go:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>Test Bed</title>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
var picArr = new Array(&quot;pic1.jpg&quot;,&quot;pic2.jpg&quot;,&quot;pic3.jpg&quot;,&quot;pic4.jpg&quot;)
picArr = picArr.sort(randomSort);

function randomSort() {
    var randResult = Math.floor(Math.random()*3)-1 //randomly -1, 0 or 1
    return randResult;
}
</script>
</head>
<body onload=&quot;alert(picArr.toString());&quot;>
Refresh for diff order.
</body>
</html>

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Awesome. Thank you Clarkin. I will try it at work tomorrow. You're right I keep forgetting Javascript is cvross-broswer.


Thank you mwolf00 for being so diligent. :)

Cheers!
 
Yipes, I do not know Javascript well enough. The example above opens an alert box with a string. What I need is for it to display the images.

same function as this but, as Clarkin suggested in a javascript so that it would be croos compatible.

<script language=vbs>
dim myPics
sub setPics()
myPics = array (&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;)
outStr = &quot;&quot;
randomize()
do while uBound(myPics) > 0
ranNum = fix(Rnd() * (uBound(myPics)+1))
outStr = outStr & &quot;<img src='&quot; & myPics(ranNum) & &quot;' title='&quot; & myPics(ranNum) & &quot;'>&quot;
resetArray(ranNum)
loop
'add remaining picture
outStr = outStr & &quot;<img src='&quot; & myPics(0) & &quot;' title='&quot; & myPics(0) & &quot;'>&quot;
document.getElementById(&quot;picPlace&quot;).innerHTML = outStr
end sub

sub resetArray(usedVal)
dim tempArr(), arrCount
arrCount = 0
for x = 0 to uBound(myPics)
if x <> usedVal then
redim preserve tempArr(arrCount)
tempArr(arrCount) = myPics(x)
arrCount = arrCount + 1
end if
next
redim myPics(uBound(tempArr))
for x = 0 to uBound(tempArr)
myPics(x) = tempArr(x)
next
end sub
</script>
<body onLoad=&quot;setPics()&quot;>
<div id=&quot;picPlace&quot;></div>


Thanks again. :)
 
Try this:
Code:
<body>
<script>
var outStr = &quot;&quot;;
for (n = 0; n < picArr.length; n++) {
  outStr += &quot;<img src='&quot; + picArr(n) + &quot;'>&quot;;
}
document.getElementById(&quot;picPlace&quot;).innerHTML = outStr;
</script>
<div id=&quot;picPlace&quot;>
</div>
..

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top