# Thread: Math.random in array and reverse order

1. Member Join Date
Feb 2010
Posts
4
Rep Power
0

## 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:

Java 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 ! 2. Senior Member Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
12

## 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--;. 3. Member Join Date
Jan 2010
Posts
19
Rep Power
0

## 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. 4. Senior Member Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
12

## The size of the array is no problem:
Java 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:
Java 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. 5. Member Join Date
Feb 2010
Posts
4
Rep Power
0

## Thank you everyone. The assignment is complete. here is the full code.

Java 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]);
}

}
}``` #### Posting Permissions

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