Results 1 to 4 of 4

Thread: Random number generation in java

  1. #1
    Join Date
    Nov 2009
    Posts
    518

    Random number generation in java

    Hi
    I am trying to create an array of 1000 random doubles. The random array doubles must be between 1.00 and 1000000.00. I know how to range the random values but I am getting an error in my program. Please check my program, I am posting it. And advice me for solving this problem.
    Code:
    double[] integers = new double[1000];
    for( i = 0;i<integers.length;i++)
    integers[i] = Random.nextDouble();

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Random number generation in java

    Hi
    I think the "i" variable in your for loop is not declared as integer. there is a problem with your for loop. I am posting the for loop, It should look something like this
    Code:
    for(int i = 0;i<integers.length;i++)
    Try this in your code and see if the problem is solved. If not then do post back. We will try to solve it.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Random number generation in java

    Hi
    Nothing seems to be wrong with code, then how come you are not getting the output. Ok, try creating a randam object before the for loop and then use rnd.nextDouble() to generate a random double to store in the array. Go through the code which I have posted below.
    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]);
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Random number generation in java

    Hi
    Thanks for your reply guys. But still I am have the same problem, do not why but it's just not working. I tried the above posted material for my program, but still it is not working. I have no clue what is wrong with this code. Thinks that Netbeans just does not like it for some reasons. Just a joke. Thanks though for replying, if any advice or suggestions do post back. Thanks in advance.

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Replies: 10
    Last Post: 08-11-2011, 10:29 AM
  3. Replies: 4
    Last Post: 27-06-2011, 10:39 PM
  4. Random number using java.util
    By New ID in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 02:22 AM
  5. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,808,001.31899 seconds with 16 queries