• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with Math.random()  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I got a problem with this code:



when I choose ChosenNumb -1, random generator should be started and type new value for ChosenNumb




but for some reason value of this field i 0 ... I wonder why
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare how you set ChosenNumb on line 24 with how you set typedNumb on line 7 or 8. There's a subtle but important difference.
 
Emilian Chmiel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
was blind but now I see ... thanks for help
 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should be using a Random object instead of Math.random().
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:Also, you should be using a Random object instead of Math.random().

Not sure that there's any "should" about it, but:

@Emilian: Charles is certainly right that Random (java.util.Random ←click) is a lot easier to use (and more flexible). In fact, these days, Math.random() uses a Random instance itself internally, so it's actually marginally slower - although on its own, that's no great reason to choose it.

Winston
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . In fact, these days, Math.random() uses a Random instance itself internally, . . .
It has done for as long as I can remember.

You can find a discussion about Math#random versus java.util.R‍andom here.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!