Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random number generator using arrays help.

 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to create 6 random numbers and print them to the screen using arrays. I need to create a static method that returns 6 random numbers. The first 5 numbers being random from 1 to 56 and the 6th number being random from 1 to 46. In the main method I need to create an int array to store the numbers from my static method and then loop through the arrays and print the numbers to the screen.

The problem I am having is getting the 6 random numbers from the static method and printing them to the screen. With the current code, I believe the memory location of the numbers are printing to the screen.

Can someone please look at my code and give me some idea of where the problem is?

(incorrect) code:



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eliot Jordon wrote:
Can someone please look at my code and give me some idea of where the problem is?


You first.

You need to TellTheDetails(←click) of exactly what problems you're having.

And welcome to the Ranch!
 
J. Kevin Robbins
Bartender
Posts: 1759
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you have two arrays called lottoNumber and you're not even using one of them. Your generateLottoNumbers method creates one of them, but what are you doing with it?

Second, you are calling the generateLottoNumbers method twice.

What results are you getting, what results do you expect, and where do you think the problem might be? Have you tried debugging to see if the values are what you expect them to be?
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Eliot Jordon wrote:
Can someone please look at my code and give me some idea of where the problem is?


You first.

You need to TellTheDetails(←click) of exactly what problems you're having.

And welcome to the Ranch!


Sorry. The problem I am having is getting the 6 random numbers from the static method and printing them to the screen. With the current code, I believe the memory location of the numbers are printing to the screen.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this line:



This is saying "print whatever comes back from the generateLottoNumbers method". Ok, so what does that method return? an integer array. Java does not know that you want EACH ELEMENT in the array printed.

If you want to print each element of the array, you will need to iterate through it and explicitly print each element.
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Look at this line:



This is saying "print whatever comes back from the generateLottoNumbers method". Ok, so what does that method return? an integer array. Java does not know that you want EACH ELEMENT in the array printed.

If you want to print each element of the array, you will need to iterate through it and explicitly print each element.



Very helpful, thank you. I'll rewrite some of my code and get back if I have more questions.

Thanks again.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you're calling generate() 6 times, but that generate() itself generates 6 numbers each time it's called. So you're actually generating 6 * 6 = 36 numbers.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't edit your original post (or really any prior post) when you update things. That makes it very hard for others to follow the flow. It even makes it hard for someone like me who's been following this thread to know what is going on. If you have new updated code, just put it in a new post and say "Here is my current code". That way anyone can come in, read the thread, and see what's going on.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you want to do is to pass the array reference into a method. inside that method, you populate it. Then when you are back in your main, you can print the elements out.

One suggestion...don't hardcode the number of items in your print method. All arrays have a .length variable. You'd do better with:

 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Please don't edit your original post (or really any prior post) when you update things. That makes it very hard for others to follow the flow. It even makes it hard for someone like me who's been following this thread to know what is going on. If you have new updated code, just put it in a new post and say "Here is my current code". That way anyone can come in, read the thread, and see what's going on.


ok, thanks.
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My new code:

This code I created below works as I want it to. It prints 6 random numbers to the screen the first 5 from 1-56 and the 6th number from 1-46.




What I am not understanding is how to put the portion of the code below into a method and use that method in main and get the same printed results. Basically I want to make the code in main method smaller.



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eliot Jordon wrote:
What I am not understanding is how to put the portion of the code below into a method and use that method in main and get the same printed results. Basically I want to make the code in main method smaller.


Two choices:

1. Method creates the array, populates it, returns it. Main then calls that method and uses its result. Main could even pass in a desired size for the new array, but start with a hardcoded size first, and the add enhancements in small pieces after you have the simple approach working.

2. Main creates the array, passes it to the method, method populates it, doesn't return anything.

In general I would prefer #1, but in a more complex real-world context, there might be an argument for #2.

So pick an approach and try it. To avoid getting flustered, do it in small pieces. First just define a method that does nothing, except maybe print out "Hey! We're in this method!" or something. Get it to compile. Then have main call the method. Then have it just create and return an array (if taking approach 1). Then move on to populating the array. Compile, test, debug each step before moving on to the next.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eliot Jordon wrote:Basically I want to make the code in main method smaller.

This is an excellent goal. Your main method as it stands has a lot of repetition. In lines 7-12, look at what is the same, and what is different. Isolate the differences and you will have an idea of how to start simplifying the code. Let me ask you first of all: are you planning to use this array for anything other than printing its values to the screen?
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
Eliot Jordon wrote:Basically I want to make the code in main method smaller.

This is an excellent goal. Your main method as it stands has a lot of repetition. In lines 7-12, look at what is the same, and what is different. Isolate the differences and you will have an idea of how to start simplifying the code. Let me ask you first of all: are you planning to use this array for anything other than printing its values to the screen?


only planning to use the array to print the values to the screen.
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Eliot Jordon wrote:
What I am not understanding is how to put the portion of the code below into a method and use that method in main and get the same printed results. Basically I want to make the code in main method smaller.


Two choices:

1. Method creates the array, populates it, returns it. Main then calls that method and uses its result. Main could even pass in a desired size for the new array, but start with a hardcoded size first, and the add enhancements in small pieces after you have the simple approach working.

2. Main creates the array, passes it to the method, method populates it, doesn't return anything.

In general I would prefer #1, but in a more complex real-world context, there might be an argument for #2.

So pick an approach and try it. To avoid getting flustered, do it in small pieces. First just define a method that does nothing, except maybe print out "Hey! We're in this method!" or something. Get it to compile. Then have main call the method. Then have it just create and return an array (if taking approach 1). Then move on to populating the array. Compile, test, debug each step before moving on to the next.



Thanks, I'll give it a shot.
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Took a break. Went back and started from scratch. Here is the new code I created. Not completely finished, but so far I think it's looking better than the original.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see 3 problems:

1. The generate() method is hardcoding 5, rather than using the array's length, both for the loop and for the final element.

2. The letter "l" is a horrible variable name. Looks too much like number 1. Same thing for uppercase "I". And uppercase "O" and 0.

3. You set i to 5, and then immediately test if it's 5. Testing it right after you set it is pointless, since you know what the value is. And setting it at all is pointless, since you know what the value is after the loop and you're just setting it to that same value that it's already holding anyway.

Otherwise it looks like you've got the right idea.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N. Jordan wrote:Took a break. Went back and started from scratch.


That's two good calls right there!
 
N. Jordan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:I see 3 problems:

1. The generate() method is hardcoding 5, rather than using the array's length, both for the loop and for the final element.

2. The letter "l" is a horrible variable name. Looks too much like number 1. Same thing for uppercase "I". And uppercase "O" and 0.

3. You set i to 5, and then immediately test if it's 5. Testing it right after you set it is pointless, since you know what the value is. And setting it at all is pointless, since you know what the value is after the loop and you're just setting it to that same value that it's already holding anyway.

Otherwise it looks like you've got the right idea.



Thanks for the tips Jeff!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Hollmann,
Your post was moved to a new topic.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N. Jordan wrote:Took a break. Went back and started from scratch. Here is the new code I created. Not completely finished, but so far I think it's looking better than the original.

Me too. However, I think you've missed out an important property about Lotto numbers: In addition to being "random", what else must they be?

Also, you still have a lot of hard coding in your method, and it's quite tightly coupled. How about something like this:then your main() might look something like:and you can use your generateLottoNumbers() method anywhere you like.

BTW, the above is simply an example (and possibly not the best). There are several ways to tackle this.
However, you do need to deal with the first point I stated.

Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:However, I think you've missed out an important property about Lotto numbers: In addition to being "random", what else must they be?

Oh, good call Winston. This will have a significant effect on the method used to "generate" the numbers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it is not “as well as” their being random, but instead of. Lottery numbers are not random, but have a different name, becuase there is a feature of randomness which lottery numbers don’t show.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic