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

01-27-2005, 07:32 PM

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

01-27-2005, 07: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

01-27-2005, 08: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?

01-27-2005, 08: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.

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