How to randomize?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cerise
    New Member
    • Dec 2007
    • 15

    How to randomize?

    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?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    One 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

    • cerise
      New Member
      • Dec 2007
      • 15

      #3
      Originally posted by sicarie
      One 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.
      Okay, thanks very much. I'll try it and post what I come up with as soon as I can, because as of yet I still haven't figured out how to make the objects (cards).

      Comment

      • cerise
        New Member
        • Dec 2007
        • 15

        #4
        Originally posted by sicarie
        One 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.
        I'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?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by cerise
          I'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?
          Have a look at the Collections.shu ffle() method. If I remember well I already
          gave you this tip before.

          kind regards,

          Jos

          Comment

          • cerise
            New Member
            • Dec 2007
            • 15

            #6
            Originally posted by JosAH
            Have 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

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by cerise
              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.
              My 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.

              kind regards,

              Jos

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by JosAH
                My 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.
                If the point of this exercise was to write code that shuffles a deck of cards, calling shuffle is a one-line solution. I would ask the instructor if that is acceptable, or if the whole point was to code your own implementation of a shuffle method!

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  If the professor doesn't accept the .shuffle(), think about how you could simulate shuffling using swapping...

                  Comment

                  • cerise
                    New Member
                    • Dec 2007
                    • 15

                    #10
                    Thank 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

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by cerise
                      Thank 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.
                      What a softie! I would have make students write their own shuffle ;-)

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        I probably would have skipped over the .shuffle() method provided because I would have wanted to write my own...

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by Ganon11
                          I probably would have skipped over the .shuffle() method provided because I would have wanted to write my own...
                          If 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

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            Originally posted by JosAH
                            If 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 ;-)
                            If you were the instructor, that would have been a very interesting class.

                            Comment

                            • blazedaces
                              Contributor
                              • May 2007
                              • 284

                              #15
                              Originally posted by Ganon11
                              If you were the instructor, that would have been a very interesting class.
                              I'd sign up for that class ...

                              Then I'd make it a bit harder for the teacher... :P

                              -blazed

                              Comment

                              Working...