aspose file tools*
The moose likes Beginning Java and the fly likes Printing Random Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing Random Values" Watch "Printing Random Values" New topic
Author

Printing Random Values

Josh Brodak
Greenhorn

Joined: Mar 24, 2013
Posts: 1
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

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

put your sysout outside for loop and check.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing Random Values
 
Similar Threads
Please help me with the logic of the code!
Random Number
cleaner code
Need definitions of nextInt(), abs() and some other questions
breaking out of while loop without condition being met?