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

1. Member
Join Date
Jan 2013
Posts
4
Rep Power
0

## [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.

Java 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.
Last edited by MagicalPeppers; 01-26-2013 at 03:34 AM.

2. ## 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.

3. Member
Join Date
Jan 2013
Posts
4
Rep Power
0

## 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
Last edited by MagicalPeppers; 01-26-2013 at 04:46 AM. Reason: typo

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

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.

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?

#### Posting Permissions

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