Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing Random Values

 
Josh Brodak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to generate random integers for a game, and then display the values as a hand for the player and the computer. The problem is that it prints out the hands multiple times; essentially leaving all values as zeros and randomizing each value in the array one at a time, and reprinting the hand as it changes each value.

Can someone point out what I am doing that would cause this, and maybe how to fix it.



And these are the results I keep getting:

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Brodak wrote:I am trying to generate random integers for a game, and then display the values as a hand for the player and the computer. The problem is that it prints out the hands multiple times; essentially leaving all values as zeros and randomizing each value in the array one at a time, and reprinting the hand as it changes each value.

Can someone point out what I am doing that would cause this, and maybe how to fix it.




And these are the results I keep getting:




Welcome to the ranch Josh. The way you have written your code, it will change only one value at a time for user and computer hand. For each iteration , "i" will have only a particular value and only that index position element will be randomized in both the arrays. If you want to generate all 5 or all n elements at random while generating a hand, you need to frame a separate logic for that so that every time you iterate from the start to end of the arrays and randomize each element in one go for each hand.
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put your sysout outside for loop and check.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic