• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Random Numbers, Why You No Work?

 
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!
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
lowercase baba
Posts: 12893
63
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.
 
Rancher
Posts: 2759
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
 
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
 
Bartender
Posts: 3323
86
  • 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.
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • 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.
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic