Results 1 to 6 of 6
  1. #1
    soldier is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default hard question .. help me .. about creating random numbers

    I want a program that create an array of random numbers between 1 and 50 , and prints a histogram which display how many integers are in each 'decade' ( use a star for each decade ), as it shown down:

    the output
    -------------------------------------------------------------
    How many random numbers?
    20

    6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15
    1-10 |******
    11-20 |***
    21-30 |****
    31-40 |****
    41-50 |***
    --------------------------------------------------------------

    plz help me I'll be thankful
    :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If this link doesn't help, then please post your best "good faith" attempt at solving this problem followed by a few specific questions (your original post above doesn't in fact have any questions, much less specific ones). Usually the more specific your question, the better we'll be able to help you.

    Again, much luck.

  4. #4
    soldier is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    thanks dear I write this program
    Java Code:
    import java.io.*;  
    import java.lang.*;  
    import java.util.Random;    
     
    class Test  
    {       
       public static void main(String[] args) throws Exception      
       { 
          System.out.println("How many random numbers");
           z=scan.nextInt();
          for(int i=1; i<z; ++i)    
          {           
             System.out.print(" " + (new Random().nextInt(1-50)+50));
          }
       }
    }
    it's not complete

    I want a program that ask me to enter # number then the program will print a line of # integer random numbers like that
    Java Code:
    6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15
    then the program will organize this # random numbers and divided it into 5 groups each group contain 10 numbers group number 1 from (1-10) ,numbers 2 from (11-20) , group numbers 2 from (21-30) group number 1 from (31-40) , group number 1 from (41-50) and and calculate how many number in each group if it's 1 number , the program will print for ex group number 1:
    Java Code:
    1-10 |*
    if it's 2 numbers , the program will print
    Java Code:
    1-10 |**
    so the out put of the program is like that
    the output
    -------------------------------------------------------------
    Java Code:
    How many random numbers?
    -------------------------------------------------------------
    after entering a number for exp. "20" the output will be
    Java Code:
    How many random numbers?
    20
    
    6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15
    1-10 |******
    11-20 |***
    21-30 |****
    31-40 |****
    41-50 |***
    -------------------------------------------------------------
    look [6 26 48 13 48 28 10 28 32 45 18 21 3 7 30 9 38 3 36 15] are random numbers..

    plz help me I want to submit my assignment tomorrow..
    my regards
    Last edited by soldier; 12-20-2009 at 10:09 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Check your for loop
    Java Code:
    for(int i=1; i<z; ++i)
    as I think that you want it to start at 0, not at 1. Try it out for small numbers. The reason is that your loop continues as long as i is < z meaning it will currently go from 1 to z-1 which is z - 1 times. You want it to loop z times and so it must go from i = 0 to i < z.

    2) You need to do more than just get your random numbers and display them. You also have to analyze them, and so to do that, you'll need to store them. I suggest that you create an int array of size z for this purpose. Shoot, your instructions tell you explicitly to do this, so I guess it's a good idea!

    3) I'm not sure if you're allowed repeats or not, but if not, you'll need to check each random number to see if it is already in the array (loop through the array for this), and if so, get another random number. You would need a loop to do this.

    4) To see how many of each number is present in each "decade", I suggest that you create another array int[5] of ints, one for each decade. Then after your random number array has been created, loop through it incrementing the proper item in the second array depending on which decade the random array item is.

    5) To draw your *, you'll need to use a for loop that print("*") and uses this second array to decide how many times to loop.

    The key here is to break the big problem up into small problems. I suggest you work out each small issue one at a time and in isolation if possible before trying to combine it all.

    Much luck!

  6. #6
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    If you have a fixed number of random numbers, you can create a list and shuffle it.

    Java Code:
    public class Numbers{	
    	
    	static List<Integer> lstNumber = new ArrayList<Integer>();
    	
    	public static void main(String[]args)throws Exception{
    		final int MAXNUMBERS = 50;
    		
    		for(int i = 0; i < MAXNUMBERS; i++){
    			lstNumber.add(new Integer(i + 1));
    		}
    		
    		Collections.shuffle(lstNumber);
    		
    		System.out.println("How many random numbers? ");
    		int numberRandon = Integer.parseInt(new Scanner(System.in).nextLine());
    		
    		for(int i = 0; i < numberRandon; i++){
    			System.out.println(lstNumber.get(i));
    		}
        }
    }
    Please don't laugh at my English... I'm trying my best! :)

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. RTP question about random numbers
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 03:58 AM
  3. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 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
  •