/    Sign up×
Community /Pin to ProfileBookmark

random number function

i am trying to put together a web site for one of my classes so my students can test themselves on tree identification. i can get the simple code to produce 10 random number and display the 10 images out of an array associated to those 10 numbers. i cannot get the code to work to test whether or not that image has been displayed. basically, i have 15 images (expanding each week and will reach 128 at end of semester) that i want to select 10 images randomly with no repeats. any suggestions on how to test for this would be great. ive tried using a for loop to test but i cannot figure out how to make it check the re-calculation of the random number.

var theImages = new Array()
theImages[0] = ‘1_1.jpg’

theImages[14] = ‘1_15.jpg’

var s = 0;
var p = theImages.length;
var preBuffer = new Array();
var theTest = new Array(10);

for (i = 0; i < p; i++){
preBuffer[i] = new Image();
preBuffer[i].src = theImages[i];
}
function showImage(){
for (j = 0; j < 10; j++){
var whichImage = Math.round(Math.random()*(p-1));
theTest[j] = whichImage;
s = 0;
for (t = 0; t < 10; t++){
if (theTest[j] == theTest[s]){
whichImage == Math.round(Math.random()
*
(p-1));
}
else{
s = (s + 1)
}
}
document.write(‘<img src=”‘+theImages[whichImage]+'”><br><hr>’);
}
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJun 16.2004 — [code=php]
var theImages = new Array()
for (var i=0;i<15;i++) {
theImages[i] = '1_'+i+'.jpg'
}

var preBuffer = new Array();
var theTest = new Array(10);

for (i = 0; i < theImages.length; i++){
preBuffer[i] = new Image();
preBuffer[i].src = theImages[i];
}

function showImage(){
for (j = 0; j < 10; j++){
var whichImage = Math.floor(Math.random()*(theImages.length));
document.write('<img src="'+theImages[whichImage]+'" /><br><hr>');
theImages.splice(whichImage,1);

}
}[/code]

The [FONT=courier new]splice()[/FONT] method simply removes the entry from the array, making it so that it cannot be selected again. Notice that the images are now named 1_0.jpg to 1_14.jpg in the script. You can change that first [FONT=courier new]for[/FONT] loop back to your manual listing of all 15 images if you'd like. I just had to do that so I could test my code with all the images defined. Hope this works for you!
Copy linkTweet thisAlerts:
@luibhauthorJun 16.2004 — thanks Exuro. works like a charm.
×

Success!

Help @luibh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...