aspose file tools*
The moose likes Beginning Java and the fly likes Random number generator using arrays help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random number generator using arrays help. " Watch "Random number generator using arrays help. " New topic
Author

Random number generator using arrays help.

N. Jordan
Greenhorn

Joined: Nov 07, 2012
Posts: 9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 16, 2010
Posts: 914
    
  13

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?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
N. Jordan
Greenhorn

Joined: Nov 07, 2012
Posts: 9
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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
N. Jordan
Greenhorn

Joined: Nov 07, 2012
Posts: 9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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

Joined: Nov 07, 2012
Posts: 9
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

Joined: Nov 07, 2012
Posts: 9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Nov 07, 2012
Posts: 9
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

Joined: Nov 07, 2012
Posts: 9
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

Joined: Nov 07, 2012
Posts: 9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

N. Jordan wrote:Took a break. Went back and started from scratch.


That's two good calls right there!
N. Jordan
Greenhorn

Joined: Nov 07, 2012
Posts: 9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

Andreas Hollmann,
Your post was moved to a new topic.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7647
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Oct 13, 2005
Posts: 38357
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random number generator using arrays help.