# Math.random in array and reverse order

• 02-16-2010, 05:24 AM
eugenechia
Math.random in array and reverse order
Hello, I have an unsolved program.

I'm required to get user input on number of random numbers to appear.
e.g if user enter 3, then 3 lines of random numbers will appear.

Thereafter, the 3 random generated numbers MUST be reversed.

Here is the required output:

Enter the number of random numbers : 3
Here are the 3 random numbers:
0.123456789
0.246891357
0.369123456
Here they are again, in reverse order:
0.369123456
0.246891357
0.123456789

Requirements:
Use Scanner class
use Math.random

here is my unfinished code:

Code:

``` import java.util.*; class RandomNumbers{         public static void main(String[] args) {                 Scanner scan = new Scanner(System.in);                 int n;                 int [] array = new int[n];                 System.out.print("Enter the number of random numbers: ");                 n = scan.nextInt();                 System.out.println("Here are the "+n+" random numbers:");                 for(int a=0; a<n.length; a++) {                         n[a]=(int)(Math.random());                         System.out.println(n[a]);                         }                 System.out.println("Here they are again, in reverse order:");                 } }```
Thank you for your help everyone !
• 02-16-2010, 12:44 PM
m00nchile
Well, you did get pretty far, just a bit more and it's done. My reccomendation is this, you use int a as the couter in your for loop, why not define it outside the loop, so you can use it after the first loop is over? That way, you already a counter that points to the end of the array, all you would have to do before using it again is a--;.
• 02-16-2010, 03:45 PM
Gilvan Justino
As the size of the array is dinamic - the user says how many numbers it must be stored - shouldn't you use ArrayList? Or there is a limit about how many numbers the user can asked?

Your code doesn't compile in my environment.
• 02-16-2010, 04:47 PM
m00nchile
The size of the array is no problem:
Code:

```Scanner s = new Scanner(System.in); System.out.println("How many numbers do you wish to generate?"); int size = s.nextInt(); double[] result = new double[size];```
Now, I'll give you a pretty big hint (to the OP), just fill in the blanks:
Code:

```//you existing code import java.util.Scanner; public class NumGen {   public static void main(String[] args) {       Scanner s = new Scanner(System.in);       System.out.println("How many numbers do you wish to generate?");       int size = s.nextInt(), counter;       double[] result = new double[size];       for(counter = 0; counter < size; couter++) {         result[counter] = Math.random();         System.out.println(result[counter]);       }       counter--;       for( ; ; ;) //just fill in this for loop         System.out.println(result[counter]);     } }```
Just figure out the logic of the second for loop, and your assignment is solved.
• 02-17-2010, 04:33 AM
eugenechia
Thank you everyone. The assignment is complete. here is the full code.

Code:

```import java.util.*; class Program1{         public static void main(String[] args) {                 Scanner scan = new Scanner(System.in);                 int n;                 System.out.print("Enter the number of random numbers: ");                 n = scan.nextInt();                 double [] array = new double[n];                 System.out.println("Here are the "+n+" random numbers:");                 for(int a=0; a<array.length; a++) {                         array[a]=(Math.random());                         System.out.println(array[a]);                         }                 System.out.println("Here they are again, in reverse order:");                 for(int a=array.length-1;a>=0; a--){                                                 System.out.println(array[a]);                                         }                 } }```