New to C here. I am trying to initialize a a string with a random selection from an array. I have hit a roadblock. Here is what I have so far and there may be a much better way to do this.
I am trying to essentially display a random card (rank and suit, kC = King of Clubs) each run.
#include <stdio.h>
#include <time.h>
#include <string.h>
int main()
{
char rank[13] = {'a','2','3','4','5','6','7','8','9','t','j','q','k'};
char suit[4] = {'C','D','H','S'};
int first;
int second;
srand(time(NULL));
first = rand()%rank;
second = rand()%suit;
printf("Your Card: %d %d", first, second);
return 0;
I suspect that rand can’t randomize an array like I am trying but is there a way to tell rand to choose from my array?
Thanks
1 Answer