• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

picking a random card from a deck

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering how we can programmatically pick a random card from a deck of 52 cards.
For simplification let the cards be integers from 1 to 52.
A card is randomly picked and set aside. So in the next round we will have only 51 cards. and so on...

Can someone solve this for me please, I couldn't
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A common way to do this is to "shuffle" the 52 cards into random order. Then you can just take the first one, second one, etc. This is so common that Sun built a shuffle method into Collections. Will that do it for you?
 
Gayatri Sinha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, ya its works.
Are there any more other ways to do this?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure... put all 52 into an array. pick one at random. swap it with the last.

then, pick one of the 51. swap it with the second to last, etc.

or...

have an array of 52 elements that keeps track of whether a card has been selected or not. pick a number from 1-52, and check to see if it's marked. if not, you're good. if it has, pick another number. note that it may take a while before you hit that last card...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[fred]:

sure... put all 52 into an array. pick one at random. swap it with the last.

then, pick one of the 51. swap it with the second to last, etc.


Note that this is pretty much what Collections.shuffle() does. Check out the source code. If you don't usually need the entire deck to be shuffled, you could modify this to just shuffle one card, each time you need a new card.

Regarding Fred's second suggestion, you could also use a Set (e.g. HashSet) rather than an array. For only 52 cards, using an array is extremely sensible. However for a similar problem with a much larger number of random elements, a HashSet may make more sense. E.g. if there are too many elements to hold them all in memory, but you draw only a few of them. The array could take much more memory than the HashSet.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic