Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Random but continue till  RSS feed

 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, I want to make it continue till i got two same random dices. But, the max is 20 trial since i dont want it roll forever.
But, the problem is, when i compile, it's only make same 20 random dices.
Do you get it? Sorry for my bad English
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randomize the Random Generator on initialization.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle Linkherz wrote:
But, the problem is, when i compile, it's only make same 20 random dices.


Well, your code roll the dice once (set to a random number), then you print the dice value 20 times. Since you never change the dice value (ie. rolled again), why would you expect the dice value to change?

Henry
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Randomize the Random Generator on initialization.


My bad, I should have looked over your code more closely... as stated, call g.nextInt(6) when ever you want a new roll.
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Gabrielle Linkherz wrote:
But, the problem is, when i compile, it's only make same 20 random dices.


Well, your code roll the dice once (set to a random number), then you print the dice value 20 times. Since you never change the dice value (ie. rolled again), why would you expect the dice value to change?

Henry


Well, the problem is i haven't known yet how to roll again ..
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
Les Morgan wrote:Randomize the Random Generator on initialization.


My bad, I should have looked over your code more closely... as stated, call g.nextInt(6) when ever you want a new roll.


Sorry, i tried to change my random g to yours and change :


but, it's same with before. it doesn't roll new ones when it's different.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle Linkherz wrote:
int a = 1+g.nextInt(6);
int b = 1+g.nextInt(6);
int c = a+b;


In that code you call g.netInt(6), which gives you a new integer between 0 and 5, you then add 1 to it and assign that value to a, then you do it again for b.

You need to do that same thing every place you want a new value for you dice. New dice values are not generated automatically, you must manually code it in "a = 1+g.nextInt(6);" and "b = 1+g.nextInt(6);" then add them for the total "c = a+b;"

please note that once you declare your variables, as you do in your code, the next calls don't use the declaration of "int" because they already have been defined.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I am trying to say:
 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I added code tags to your post; always use the tags. Doesn't it look better.
I also changed the coloured text to black because some people find colours difficult to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!