Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-14-2012, 02:01 AM   PM User | #1
sg123
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
sg123 is an unknown quantity at this point
Question Help with multiple random images

Here's the code I slightly modified from the source found on javascriptkit.com:

Code:
<script>
function random_imglink(){
  var myimages = new Array()
    myimages[1] = "01.jpg"
    myimages[2] = "02.jpg"
    myimages[3] = "03.jpg"
    myimages[4] = "04.jpg"
    myimages[5] = "05.jpg"
    ...
    myimages[31] = "31.jpg"
							
  var rq=Math.floor(Math.random()*myimages.length)
  var rr=Math.floor(Math.random()*myimages.length)
  var rs=Math.floor(Math.random()*myimages.length)
  var rt=Math.floor(Math.random()*myimages.length)
  var ru=Math.floor(Math.random()*myimages.length)
  var rv=Math.floor(Math.random()*myimages.length)
  var rw=Math.floor(Math.random()*myimages.length)
  var rx=Math.floor(Math.random()*myimages.length)
  var ry=Math.floor(Math.random()*myimages.length)
  var rz=Math.floor(Math.random()*myimages.length)

  if (ry==0)
    ry=1
    document.write('<img src="'+myimages[rq]+'"> &nbsp;<img src="'+myimages[rr]+'"><br><img src="'+myimages[rs]+'"> &nbsp;<img src="'+myimages[rt]+'"><br><img src="'+myimages[ru]+'"> &nbsp;<img src="'+myimages[rv]+'"><br><img src="'+myimages[rw]+'"> &nbsp;<img src="'+myimages[rx]+'"><br><img src="'+myimages[ry]+'"> &nbsp;<img src="'+myimages[rz]+'">')
}
random_imglink()
</script>
I have 8 IMGs grabbing images randomly from a list of 31 JPGs & here's my dilemma:
1. I don't want the same image appearing in more than 1 spot
2. When I test this, sometimes 1 or more IMG spots return an undefined image

Any help would be greatly appreciated. Thanks in advance!
sg123 is offline   Reply With Quote
Old 06-14-2012, 03:02 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Shuffle the arrat and then grab as many as you need off the front.


Code:
Array.prototype.shuffle = function() {
   var s = [];
   while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
   while (s.length) this.push(s.pop());
   return this;
}; 

function random_imglink(){
  var myimages = []
    myimages[1] = "01.jpg"
    myimages[2] = "02.jpg"
    myimages[3] = "03.jpg"
    myimages[4] = "04.jpg"
    myimages[5] = "05.jpg"
    ...
    myimages[31] = "31.jpg"

myimages.shuffle();
   
    var rq = myimages[0];
    var rr = myimages[1];
// ... etc
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
sg123 (06-14-2012)
Old 06-14-2012, 05:11 AM   PM User | #3
sg123
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
sg123 is an unknown quantity at this point
That worked perfectly!

Here's the fixed code block in case someone else is looking for the same solution:

Code:
Array.prototype.shuffle = function(){
	var s = [];
	while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
	while (s.length) this.push(s.pop());
	return this;
};
function random_imglink(){
	var myimages = []
		myimages[0] = "01.jpg"
		myimages[1] = "02.jpg"
		myimages[2] = "03.jpg"
		myimages[3] = "04.jpg"
                ...
		myimages[30] = "31.jpg"

		myimages.shuffle();

		var rq = myimages[0];
		var rr = myimages[1];
		var rs = myimages[2];
                ...
		var rz = myimages[9];

	document.write('<img src="images/thumbnails/'+rq+'"> &nbsp;<img src="images/thumbnails/'+rr+'"><br><img src="images/thumbnails/'+rs+'"> &nbsp;<img src="images/thumbnails/'+rt+'"><br><img src="images/thumbnails/'+ru+'"> &nbsp;<img src="images/thumbnails/'+rv+'"><br><img src="images/thumbnails/'+rw+'"> &nbsp;<img src="images/thumbnails/'+rx+'"><br><img src="images/thumbnails/'+ry+'"> &nbsp;<img src="images/thumbnails/'+rz+'">')
}
random_imglink()
sg123 is offline   Reply With Quote
Reply

Bookmarks

Tags
image, javascript, problem, random

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 AM.


Advertisement
Log in to turn off these ads.