• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Random but continue till

 
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Randomize the Random Generator on initialization.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I am trying to say:
 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
See where your hand is? Not there. It's next to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic