Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I create an array with random numbers?

    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

  • #2
    New Coder
    Join Date
    Dec 2003
    Location
    At work...still
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try something like this

    Code:
    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
    Why?
    Ok, but Why?

  • #3
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks it worked.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •