#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Function to get random two consecutive digits from a number
void getRandomTwoDigits(int num, char *out) {
char s[16]; // buffer to hold number as string
sprintf(s, "%d", num);
int len = strlen(s);
if (len < 2) {
strcpy(out, "Err"); // not enough digits
return;
}
int start = rand() % (len - 1);
strncpy(out, s + start, 2);
out[2] = '\0';
}
int main(void) {
srand((unsigned)time(NULL));
int num = 123456;
char randomTwo[3];
getRandomTwoDigits(num, randomTwo);
printf("Random two digits: %s\n", randomTwo);
return 0;
}
/*
run:
Random two digits: 23
*/