Results 1 to 10 of 10
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Random number generator

    lets say i have a number 496574... I want add up all of those numbers, so 4+9+6+5+7+4 = 30. Next I want to check if it is divisible by 3. If it is, then done. Otherwise I want to arrange the numbers in order from greatest to least and take out the smallest possible number so that it is divisible by 3.

    For instance 468793 = 37... So now I want to sort that from greatest to least -> 987643 and then I want to remove 1 or more numbers so that it is the greatest number possible and divisible by 3. In this case we would remove the 4 to get 98763 = 33 which is divisible by 3. That is the ONLY correct answer. taking out the 7 would work too, however, I want the greatest number that we can produce with those digits, and 98643 would be wrong seeing as 98763 is greater. That is what I want to accomplish with this.

    So, I created a random number generator to produce a 6 digit number, but I feel like I am doing it wrong for what I want to use it for.
    I want to create a 6 digit number where I can add up all of the digits to produce some number and see whether or not that is divisible by 3.

    The way I did this was...
    Java Code:
    int[] x = new int[6];
    		
    		public void randomNumber()
    		{
    			Random generator = new Random();
    			for(int i = 0; i<6; i++)
    			{
    				int randomInt = generator.nextInt(10);
    				System.out.print(x[i] = randomInt);
    			}
    do you guys think this is a good way to go about doing this or do you think there is a better way to help me this number be more manageable. Like I want to be able to take this number and know the digits in it, and to see if it isn't divisible by 3, what digit(s) I need to remove in order to make it divisible by 3
    Last edited by adjit; 03-05-2012 at 02:59 PM.

  2. #2
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Random number generator

    any help please?

  3. #3
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: Random number generator

    Java Code:
    public class test42 {
    	
    	public static void main(String[] args) {
    		String number = "";
    		int temp;
    		Random x = new Random();
    		for (int i = 0; i < 6; i++) {
    			String anumber = Integer.toString(x.nextInt(10));
    			number = number + anumber;
    		}
    		temp = Integer.parseInt(number);  // now you have a 6 digit int
    		
    			
    		}
    
    }
    your way you create 6 numbers that are all one digit but are not 1 six digit number

  4. #4
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Random number generator

    so now how would I go about adding up all of the digits in that number? and then sorting the number from greatest to least?

  5. #5
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Random number generator

    You might be able to add all of those digits together in a string, then parse it after the loop? If I'm understanding what you want to do correctly...

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Random number generator

    lets say i have a number 496574... I want add up all of those numbers, so 4+9+6+5+7+4 = 30. Next I want to check if it is divisible by 3. If it is, then done. Otherwise I want to arrange the numbers in order from greatest to least and take out the smallest possible number so that it is divisible by 3.

    For instance 468793 = 37... So now I want to sort that from greatest to least -> 987643 and then I want to remove 1 or more numbers so that it is the greatest number possible and divisible by 3. In this case we would remove the 4 to get 98763 = 33 which is divisible by 3. That is the ONLY correct answer. taking out the 7 would work too, however, I want the greatest number that we can produce with those digits, and 98643 would be wrong seeing as 98763 is greater. That is what I want to accomplish with this.
    Last edited by adjit; 03-05-2012 at 03:00 PM.

  7. #7
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Random number generator

    anymore ideas?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Random number generator

    You've got your number now, so how do you think you should go about it?
    Bestsanchez has given an idea of a possible solution involving Strings.
    You might choose to store the 6 digits in a different way than in a String.
    However you choose to go you need 6 digits that you can manipulate.

    I'd suggest figuring out how you would do this by hand and then translate that into code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Random number generator

    So, what I did is create a array of length 6 and put 6 random numbers in there. Next I checked to see if a[I] +.... Etc add up to a number that is divisible by 3. If it was then stop, otherwise I sorted the array using arrays.sort. that gives the number to me sorted from least to greatest, not what I need, so i assigned each number to variables a-f assigning them from greatest to least. Next what I was thinking about making an if statement asking if the remainder is 1 (of a+...+f) search the numbers for 1,4,and 7.

    Or what about a while(true) then have the if statements in that and switch to false if you find that number

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Random number generator

    You could (I think) have used a Comparator<Integer> to compare in reverse, though you might have to change your array to Integer[] rather than int[].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Random Phone number generator
    By elecleoalune in forum New To Java
    Replies: 15
    Last Post: 05-19-2014, 12:37 PM
  2. Random number generator with range.
    By hifriend in forum New To Java
    Replies: 2
    Last Post: 11-15-2011, 08:12 PM
  3. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 08:18 PM
  4. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 12:10 PM
  5. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM

Posting Permissions

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