This is an Age Old Amazon Question ..

int main(void)
  {
    int card[52];
    int n;
    srand(time(0));
    for(int i=0;i<52;i++)
    card[i]=i;
    while(cin>>n) {
        for(int i=0;i<(52-i);i++) {
                int r=i+(rand()%(52-i));
                int temp=card[i];
                card[i]=card[r];
                card[r]=temp;
                }
        for(int c=0;c<n;c++)
                cout<<card[c]<<" ";
        cout<<endl;
    }
    return 0;
  }


Jst For Reference if Anyone Needs...

On Wed, May 23, 2012 at 2:40 PM, Ramindar Singh <ramin...@gmail.com> wrote:

> Write a method to shuffle a deck of cards. It must be a perfect shuffle -
> in other words, each 52! permutations of the deck has to be equally likely.
> Assume that you are given a random number generator which is 
> perfect.<http://shashank7s.blogspot.com/2012/02/write-method-to-shuffle-deck-of-cards.html>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/algogeeks/-/kNoMc6CLVb4J.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to