• Post Reply Bookmark Topic Watch Topic
  • New Topic

integer needed  RSS feed

 
james john jason lee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need a little help at my 2nd day with Java.. - why does this little code doesn't give me an integer? Everytime I click the Button it shows me '0'. But if I change the type to a double for example, it works fine and I get a double. Why?



Thanks in advance,
Hank
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using Random.nextInt(n) instead. It is recommended to use it anyways instead of Math.random().
Usage: Random.nextInt(10) should give you a random integer from 0-9. I believe anyway.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If it binds this way:



then (int)Math.random() will always be zero due of random()'s returning a floating point value between 0 and 1.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!