aspose file tools*
The moose likes Beginning Java and the fly likes Random Numbers, Why You No Work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Numbers, Why You No Work?" Watch "Random Numbers, Why You No Work?" New topic
Author

Random Numbers, Why You No Work?

Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Hey guys, another question. So in my oh so spectacular book, (Head First Java) I'm around the part called "simple dot com game", and before I moved on to the next section, I wanted to write a small program experimenting the following concepts:

Dealing with arrays
Passing and calling
generating random numbers
enhanced for loops
encapsulation (Not yet implemented)

So, yeah. Why am I getting this result from this code? All I want is it to print out 10 random integers from one to 7.






While it compiles, it gives me an output like this:

0
0
0
0
0
0
0
0
0
0

Yep, that's all.
I think its a rounding issue. Can someone help me? (Keep in mind, I am EXTREMELY NEW to coding.)

Thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Kevin Corina wrote:Hey guys, another question. So in my oh so spectacular book, (Head First Java) I'm around the part called "simple dot com game", and before I moved on to the next section, I wanted to write a small program experimenting the following concepts:

Dealing with arrays
Passing and calling
generating random numbers
enhanced for loops
encapsulation (Not yet implemented)

So, yeah. Why am I getting this result from this code? All I want is it to print out 10 random integers from one to 7.






While it compiles, it gives me an output like this:

0
0
0
0
0
0
0
0
0
0

Yep, that's all.
I think its a rounding issue. Can someone help me? (Keep in mind, I am EXTREMELY NEW to coding.)



Question one: How many random numbers do you think are generated?



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


Question two: What do you think this loop does? Or in other words, what are the values of "i" as the loop is iterating?



Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


But to answer your question...

Kevin Corina wrote:
I think its a rounding issue. Can someone help me? (Keep in mind, I am EXTREMELY NEW to coding.)



Well, there may be a rounding issue in your program... not exactly sure. Regardless, rounding errors is *not* the reason you are only printing zeros.

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

One of the best things you can do is stick System.out.println() statements in your code. For example, I might start with this:


Now, not only can I see what the number I'm returning is, but I can also see how many times the makeSomeNumbers() method is REALLY being called.


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

Joined: Nov 29, 2012
Posts: 14
Thanks guys! Sorry I took so long to reply, I completely forgot about this thread. I actually completely scrapped the integer array and made it an array of objects that held, and returned, the numbers. I also reworked some of the for loop. Thanks for your help anyways, this community is pretty great.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2448
    
  28

Well, yeah, we want people to help people to solve their problems, so it's all good. It would be nicer if you can post how you solved your problem. That would help people who get a similar problem in the future
adam ward
Greenhorn

Joined: Dec 12, 2012
Posts: 1
Math.radom() is random number lessthan 0 ( ex. 0.017937,0.023728 ) if you cast type to int you must *100
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2417
    
  50
Welcome to the ranch Adam.

Math.radom() is random number lessthan 0 ( ex. 0.017937,0.023728 )

I think you meant less than 1 rather than less than 0.
The exact definition is it returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0

if you cast type to int you must *100

You are right in that before you cast it to an int you must multiply it by the number that specifies the range you want or you will always end up with 0. However multiplying by 100 is only useful if you want random numbers in the range of 0 to 99.
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 153

Since it hasn't been mentioned, class java.util.Random may prove easier to use for acquiring a random integer between 0 and 7.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random Numbers, Why You No Work?