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

Random Numbers, Why You No Work?

 
Kevin Corina
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12125
30
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kevin Corina
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.radom() is random number lessthan 0 ( ex. 0.017937,0.023728 ) if you cast type to int you must *100
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it hasn't been mentioned, class java.util.Random may prove easier to use for acquiring a random integer between 0 and 7.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic