Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 7th, 2008, 4:53 AM   #1
Smessed-Up
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 Smessed-Up is on a distinguished road
Random image array

basically once one of the images is created randomly i want it removed from the array so it cant come up again but for some reason not all the images are appearing anyway heres what ive got whats wrong/missing?

[code]var image=new Array();
var g='.gif';
image[0]='image1'+g;
image[1]='image2'+g;
image[2]='image3'+g;
image[3]='image4'+g;
image[4]='image5'+g;
var l=image.length;
var buff=new Array();
for(i=0;i<l;i++){

buff[i]=new Image();
buff[i].src=image[i];

}

var randomNumber=Math.floor(Math.random()*l);
for(i=0;i<l;i++){

document.write('<img src="'+image[randomNumber]+'">');
image.splice(randomNumber,1);

}
Smessed-Up is offline   Reply With Quote
Old Mar 7th, 2008, 5:38 PM   #2
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 369
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Random image array

If you want to display all 5 images it's easier just to do the following:
<script type="text/javascript">
<!--
	for(var i = 0; i < 5; i++) {
		document.write('<img src="image"' + (i + 1) + '.gif">');
	}
//-->
</script>
Also be sure to properly enclose your code in code tags.
__________________
You are here to serve the board. So post well... and live. SPAMMING SPEED! GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 7th, 2008, 5:59 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 369
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Random image array

If you want them to appear in a random order, for whatever reason:
<script type="text/javascript">
<!--
	var imgCount = 5;
	var image = new Array();
	for(var i = 0; i < imgCount; i++) {
		image.push('image' + (i + 1) + '.gif');
	}
	while(image.length > 0){
		var randomIndex = Math.floor(Math.random() * image.length);
		document.write('<img src="' + image[randomIndex] + '" alt="' + image[randomIndex] + '">');
		image.splice(randomIndex, 1);
	}
//-->
</script>
The issue with your code is that when you splice the array you're not taking into account the fact that the array size is changing dynamically. Therefore, you're generating non-existent indices. Whenever you splice an array you're going to be shortening its length, and the result will have new indices. Thus, when you have 5 elements your random number generation needs to be from 0 to 4, when you remove one and have 4 elements it needs to be from 0 to 3, and so on, until you have no more elements left in the array.
__________________
You are here to serve the board. So post well... and live. SPAMMING SPEED! GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 7th, 2008, 7:40 PM   #4
Smessed-Up
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 Smessed-Up is on a distinguished road
Re: Random image array

Ah i see now thankyou for your help.
Smessed-Up is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
ADA Random Seed of an Array scm007 Other Programming Languages 1 Mar 25th, 2007 9:05 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
random numbers in 2D array cwl157 Java 4 Apr 29th, 2005 6:08 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
Checking source codes of image, audio and video files on_auc C++ 3 Feb 21st, 2005 8:36 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC