# Random number generator

• 03-04-2012, 10:35 PM
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...
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
• 03-05-2012, 03:08 AM
Re: Random number generator
• 03-05-2012, 03:44 AM
Danieldcc
Re: Random number generator
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
• 03-05-2012, 03:55 AM
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?
• 03-05-2012, 04:42 AM
Bestsanchez
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...
• 03-05-2012, 05:02 AM
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.
• 03-05-2012, 03:59 PM
Re: Random number generator
anymore ideas?
• 03-05-2012, 04:13 PM
Tolls
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.
• 03-05-2012, 04:37 PM