Results 1 to 10 of 10
- 11-16-2009, 03:52 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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();
- 11-16-2009, 04:04 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
The "i" variable in your for loop hasn't been declared as an int.
- 11-16-2009, 04:32 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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.
- 11-16-2009, 04:37 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
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++)
- 11-16-2009, 04:56 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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();
- 11-16-2009, 05:32 AM #6
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
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]); }
- 11-16-2009, 06:13 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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.
- 11-16-2009, 08:58 AM #8
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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 09:02 AM.
- 11-16-2009, 11:57 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Again, the "i" variable needs to be declared as an int, not a double. Try it yourself and see what happens.
- 11-16-2009, 11:15 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Random number generation
By toasty in forum New To JavaReplies: 1Last Post: 09-30-2009, 11:41 AM -
Random number generater
By kickflipper1087 in forum New To JavaReplies: 6Last Post: 10-21-2008, 10:19 PM -
Random number help
By jgonzalez14 in forum New To JavaReplies: 5Last Post: 09-16-2008, 09:13 AM -
Random number
By jithan in forum Advanced JavaReplies: 1Last Post: 06-13-2008, 01:42 PM -
random generation
By carlos123 in forum New To JavaReplies: 10Last Post: 01-09-2008, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks