• Post Reply Bookmark Topic Watch Topic
  • New Topic

(int) can't be applied to Math.random?  RSS feed

 
Bear Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to make a random number an integer, am I putting the (int) in the wrong place?



Thanks!
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's the right place.

Although if you take a random number in the range [0, 1) and truncate it to the next lower integer, you're always going to get zero. Was that the reason you asked the question? Or did you ask because you got a compiler error? I see both problems in your code, but in future it would help if you would TellTheDetails (read that link) to save people time in answering.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Kim wrote:Hi, I'm trying to make a random number an integer, am I putting the (int) in the wrong place?



Thanks!


Yeah, what he said. TellTheDetails.

However, when you look at the documentation for Math.random(), what made you think you would be passing an int to it?

Also, note that the error message is telling you exactly what you're doing wrong.

Finally, I would recommend you use java.util.Random.nextInt(int)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!