I'm just starting learning Java, and we have a project that needs to demonstrate shuffling playing cards. We're using objects as our playing cards, and we've put these objects in stacks, and I wonder if anyone can help me how to "shuffle" these objects or put them in a random arrangement?
How to randomize?
Collapse
X
-
-
Originally posted by sicarieOne of the best skills you can develop is the ability to search for methods and classes that you can use. So check this out and see if you can figure out a way to do that. If not, post again with what you tried, we'll try to get you headed in the right direction.Comment
-
Originally posted by sicarieOne of the best skills you can develop is the ability to search for methods and classes that you can use. So check this out and see if you can figure out a way to do that. If not, post again with what you tried, we'll try to get you headed in the right direction.Comment
-
Originally posted by ceriseI've figured out how to use Math.random() package, but is there a way to make the generated numbers unique? For example, I only need numbers from 0 to 51. Can you tell me how I generate one random number only once so that in the end, it seems like I'm rearranging the numbers 0 to 51 in a random sequence?
gave you this tip before.
kind regards,
JosComment
-
Originally posted by JosAHHave a look at the Collections.shu ffle() method. If I remember well I already
gave you this tip before.
kind regards,
Jos
Thank you. ^_^ But I'm pretty sure you haven't given me this tip yet, as it is the first time I've read or heard of it.Comment
-
Originally posted by ceriseThank you. ^_^ But I'm pretty sure you haven't given me this tip yet, as it is the first time I've read or heard of it.
lists for you such that every permutation of the list have equal chance. Simply
create a list with unique numbers and let that method do the hard work.
kind regards,
JosComment
-
Originally posted by JosAHMy bad; it must've been someone else. The shuffle() algorithm correctly shuffles
lists for you such that every permutation of the list have equal chance. Simply
create a list with unique numbers and let that method do the hard work.Comment
-
Originally posted by ceriseThank you everyone for your very helpful replies. ^^ I was able to shuffle the cards now using the shuffle() algorithm. Our instructor accepted this method of shuffling the cards since the overall project is solitaire.Comment
-
Originally posted by Ganon11I probably would have skipped over the .shuffle() method provided because I would have wanted to write my own...
I sincerely hate having to dig through all those off-by-one errors.
kind regards,
Jos ;-)Comment
-
Originally posted by JosAHIf I were the instructor I would've accepted that Collections.shu ffle() method because
I sincerely hate having to dig through all those off-by-one errors.
kind regards,
Jos ;-)Comment
-
Originally posted by Ganon11If you were the instructor, that would have been a very interesting class.
Then I'd make it a bit harder for the teacher... :P
-blazedComment
Comment