lundi 1 novembre 2021

Function to pick a seemingly random index from an array

I'm having a problem figuring out how to pick a random index from an array<string>.

It's for a card game. Instead of shuffling the deck I wanna use the srand function, inside a function to seemingly pick a random number. But every thing that I try just fails.

Here is part of the array:

array<string, 52> cards = { "Ess \3", "Ess \4", "Ess \5", "Ess \6","2 \3","2 \4" };

The \3 \4 \5 \6 is the symbols of the cards, for example Hearts, Diamond, Spades.

Anyway.. I just wanna pick a seemingly random index of the 52. When I think I'm getting close I run in to something like this: "C++ no suitable constructor exists to convert from int to basic_string" something like that.

And I don't get it. I have tried for a very long time. Please help me.

My latest attempt...

string randindex = rand() % 52;
cout << cards[randindex];

This is of course not working for me. Before creating a function i first have to learn how to pick a random index...




Aucun commentaire:

Enregistrer un commentaire