Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem generating random number with a class  RSS feed

 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is a basic program "Coin Tossing" I'm working on to practice generating random numbers with a class. I send 1000 coin tosses to a class, which then generates either a 1 (for heads) or 2(for tails) for 1000 iterations, and then returns the number of heads to my main class. Here is the problem:

Every time I run the program the output is 2 for heads and 998 for tails! I can't figure out why. For a test case I wrote a program using .Random but without sending anything outside Main and it works just fine. Any help is appreciated: I'm not getting any error messages, so I assume my problem is a logic problem.

Here is my main class:



Here is my 'Coin' class:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're using 'heads' for both determination (1 or 2), and for the total number of heads,
so it won't work. try these changes:

//int heads = 0;
int headsTotal = 0;

//heads = random.nextInt(2) + 1; // flip coin
int heads = random.nextInt(2) + 1; // flip coin

//heads += 1;
headsTotal += 1;

//return heads;
return headsTotal;
 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael...that worked great. And for me a nice lesson learned.

Thanks,
Bill
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would also have avoided this problem if you had used "random.nextBoolean()" to get a random true or false value. Then your core code would look something like this:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Suttle wrote:And for me a nice lesson learned.

I hope the "nice lesson learned" was to use good, clear variable names that really describe what they represent...
 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice move Paul. More elegant than my approach. Yes Fred, clear variable descriptions would have helped me see that I was trying to use one variable for both the determination of each "coin roll" and the total...which are clearly distinct. Thanks!!
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Suttle wrote:Thanks Michael...that worked great. And for me a nice lesson learned.

Well here's another one then. Since the value of the toss is exclusive, you could use an array of counts with no extra "mucking about":the extra method is just there for flexibility. For example, with it you could also add a getTails() method.

Winston

[Edit] Ooops. Forgot the 'public' on the methods.
 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!