Results 1 to 4 of 4
- 01-26-2013, 02:09 AM #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.
Problems: Clunky codeJava 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" } } }
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 02:34 AM.
-
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, 03:42 AM #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. :DLast edited by MagicalPeppers; 01-26-2013 at 03:46 AM. Reason: typo
-
Re: [Help] Add "random" numbers to an array. A easy to understand thread.
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.
What is the purpose of the while loop that wraps your for loop? Why not simply have the for loop on its own?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.
Similar Threads
-
Error: "Exception in thread "main" java.util.NoSuchElementException"
By Mattiscool in forum New To JavaReplies: 1Last Post: 11-02-2012, 11:38 PM -
Using a "Random" method to print out random index of Array List?
By RarkMowe in forum New To JavaReplies: 5Last Post: 10-24-2012, 09:17 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
"Exception in thread "Launcher:/Whiteboard" java.lang.RuntimeException: Failed to loa
By Shajith in forum EclipseReplies: 3Last Post: 03-21-2011, 01:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks