Results 1 to 5 of 5
  1. #1
    eugenechia is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default 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. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

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

    Default

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

    Default

    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. #5
    eugenechia is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

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

Similar Threads

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-26-2011, 12:10 AM
  2. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 02:26 AM
  3. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 10:28 AM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 04:31 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
  •