# [Help] Add "random" numbers to an array. A easy to understand thread.

• 01-26-2013, 03:09 AM
MagicalPeppers
[Help] Add "random" numbers to an array. A easy to understand thread.
Hello everyone,

I am still learning the java language, and prolly will be for the rest of my life (as it updates). Today I have written some code to generate random numbers in an array. Although, the code is really clunky and I know it is not the best method of doing what I am trying to do. Without further ado, here is my Random Array/Lotto number program.

Code:

``` public class main {         public static void main(String[] args){                                 boolean isSet = true;                 int lottoNums[] = {0,0,0,0,0};                                 do {                         isSet = true;                         for(int i = 0; i < lottoNums.length; i++){                                 if(lottoNums[i] == 0) {                                         double randomNum = (Math.random() * 55) +1; //generate random num. add 1 to prevent "0"                                         int newNum = (int)randomNum; //convert the double to an int.                                         lottoNums[i] = newNum;                                                                                 isSet = false;                                 }                         }                 } while (!isSet);                                 for(int i = 0; i < lottoNums.length; i++) {                         System.out.print(lottoNums[i] + ","); //print the "lotto numbers"                                        }         } }```
Problems: Clunky code
Does Compile: Yes
Works Correctly: Yes

If someone can, please comment on my code (as a beginner) and let me know how I can improve.

(this program emulates the texas lottery, in that it generates random "lotto" numbers). This was made for educational purposes and is not meant to be a program to help with the lottery.
• 01-26-2013, 04:35 AM
Fubarable
Re: [Help] Add "random" numbers to an array. A easy to understand thread.
The main problem that I see is that your code could allow duplicate numbers, something that a lottery wouldn't allow.
• 01-26-2013, 04:42 AM
MagicalPeppers
Re: [Help] Add "random" numbers to an array. A easy to understand thread.
I was not aware that the lotto didn't allow duplicate numbers. And now that I think about it, I see why (easier chance of winning). That being said, this for me is more about learning. Other than that flaw, is this a decent solution at generating random array numbers, or is there a better way.

I apologize if this is vague. Also, thank you for your reply, I do appreciate all input. :D
• 01-26-2013, 04:49 AM
Fubarable
Re: [Help] Add "random" numbers to an array. A easy to understand thread.
Quote:

Originally Posted by MagicalPeppers
I was not aware that the lotto didn't allow duplicate numbers. And now that I think about it, I see why (easier chance of winning).

No, I think that it's more the reality of the picking numbers. It's more closely related to picking random cards from a deck then choosing random numbers, since once a card has been chosen, it is removed from the deck. If you see the way most lotteries work with balls moving about randomly in an air-filled container, each possible number is represented by a ball, and when a ball is selected, it is removed from the pool.

Quote:

That being said, this for me is more about learning. Other than that flaw, is this a decent solution at generating random array numbers, or is there a better way.
What is the purpose of the while loop that wraps your for loop? Why not simply have the for loop on its own?