random cards

Apr 4, 2016 at 2:44pm
this is my function to generate random cards.

string facevalue[] = { "DA","DK","DQ","DJ","D2","D3","D4","D5","D6","D7","D8","D9","D10",
"HA","HK","HQ","HJ","H2","H3","H4","H5","H6","H7","H8","H9","H10",
"SA","SK","SQ","SJ","S2","S3","S4","S5","S6","S7","S8","S9","S10",
"CA","CK","CQ","CJ","C2","C3","C4","C5","C6","C7","C8","C9","C10"
};


string getcard() {

string card;
int cardvalue = rand() % ARRAY_SIZE(facevalue);
card += facevalue[cardvalue];
return card;
}


so my problem is that if i save these cards inside 4 different string vectors and each vector will take 7 cards.sometimes i get repeated cards. But i dont want it repeated.How can i do that? please help. plus give me the code because i am new to coding.
Last edited on Apr 4, 2016 at 2:58pm
Apr 4, 2016 at 3:17pm
one option would be to remove each element from facevalue as they are "dealt out" to the individual vectors..

reference this post -> http://www.cplusplus.com/forum/beginner/197/
Apr 4, 2016 at 3:36pm
please help. I didnot understand the logic of that explanation.
Last edited on Apr 4, 2016 at 6:28pm
Apr 4, 2016 at 7:33pm
I think the best way to do this is by shuffling your cards - which is what happens in real life.

By shuffling, you're guaranteed unique cards with no duplicates, since all that's changing is their position. You're then free to draw as many cards as you please.
Apr 4, 2016 at 10:23pm
ok. But how do i do write it in codes?
Apr 4, 2016 at 10:51pm
Topic archived. No new replies allowed.