Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Exclamation Trouble with Random number generation

    I need to create an array of 1000 random doubles. These doubles must be between 1.00 and 1,000,000.00. Here is my code for generating random numbers into the array without regard to the range of the values. I know how to range the values but i'm getting an error in the following code.

    double[] integers = new double[1000];
    for( i = 0;i<integers.length;i++)
    integers[i] = Random.nextDouble();

  2. #2
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    The "i" variable in your for loop hasn't been declared as an int.

  3. #3
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    actually it needs to be a double and the integers[] should actually be called doubles[]. but even when i declare "i" as a double it gives me this error message: cannot find symbol: class "i". And Netbeans tries to create a class called i even when i is just a local variable.

  4. #4
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Yeah, that's another mistake both of us overlooked. Also, what I meant in my first post was that your for loop should look like this...

    for(int i = 0;i<integers.length;i++)

  5. #5
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    yeah i'm saying that even when i do declare that exact code then it comes up with the same error message. Can you put that code in an environment and have it work? Mine will not compile it and the code looks right as far as i can see. is there anything wrong with the following loop?

    for(double i; i<doubles.length;i++)
    doubles[i] = Random.nextDouble();

  6. #6
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Tried and tested. Everything seems to be working fine for me.

    Try creating a Random object before the for loop and then using rnd.nextDouble() to generate a random double to store in the array.

    Java Code:
    Double[] doubles = new Double[1000];
    Random rnd = new Random();
    
    for(int i=0; i<doubles.length;i++) {
    	doubles[i] = rnd.nextDouble(); 
    	System.out.println(doubles[i]);
    }

  7. #7
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    Yeah I do this and it still gives me an error. I have no clue what is wrong with this code. Netbeans just doesn't like it for some reason.

  8. #8
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    This is the full code that I used maybe it will help clear some things up. I just don't see the error. :


    import java.util.Random;
    public class accountToolsFarmer {
    double[] doubles = new double[1000];
    Random rnd = new Random();

    public void createArray(){
    for(double i=0.0; i<doubles.length;i++) {
    doubles[i] = rnd.nextDouble();//this is where the error is. Its saying double found, int required
    }
    }
    }
    Last edited by SteroidalPsycho; 11-16-2009 at 10:02 AM.

  9. #9
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Again, the "i" variable needs to be declared as an int, not a double. Try it yourself and see what happens.

  10. #10
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    thanks for your help! This solution worked and I should have known this.

Similar Threads

  1. Random number generation
    By toasty in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 12:41 PM
  2. Random number generater
    By kickflipper1087 in forum New To Java
    Replies: 6
    Last Post: 10-21-2008, 11:19 PM
  3. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 10:13 AM
  4. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 02:42 PM
  5. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 04:43 AM

Posting Permissions

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