Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Question [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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default 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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by MagicalPeppers View Post
    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?

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2012, 12:38 AM
  2. Replies: 5
    Last Post: 10-24-2012, 10:17 PM
  3. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  4. Replies: 14
    Last Post: 04-08-2011, 07:01 PM
  5. Replies: 3
    Last Post: 03-21-2011, 02:48 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
  •