...

View Full Version : How do I create an array with random numbers?



ilmus
01-27-2005, 06:32 PM
Hi,

I'm using the following code to create an array with 3 random integers. I'm not sure if it is working but if it is the array might contain duplicates. Can someone please suggest a way to check the array for duplicates and replace them with new entries?

var picnums = new array()
for (x=0; x<3; x++){
var picnums[x]=Math.floor(Math.random() *8)
}


Many Thanks
ilmus

WillGibson
01-27-2005, 06:49 PM
try something like this



var picnums = new array();
var foundunused = false;
for (x=0; x<3; x++){
while (!foundunused ) {
var picnums[x]=Math.floor(Math.random() *8);
foundunused = true;
for (z=0; z<x; z++){
if (picnums[x] == picnums[z]) {
foundunused = false;
}
}
}
}


thats just off the top of my head btw

ilmus
01-27-2005, 07:18 PM
Thanks a lot.

I tried it out but am getting this error: Error 1026 expected ";" for the line var picnums[x]=Math.floor(Math.random()*8);

any idea why?

requestcode
01-27-2005, 07:33 PM
Try removing the word "var" like this:
picnums[x]=Math.floor(Math.random() *8);

It is already declared above so you should not have to do it again.

ilmus
01-27-2005, 07:40 PM
Thanks it worked. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum