生成随机字符串,帮帮忙!
生成一下下面的格式,前面的000也要保留!
格式为:360781198510295118
其中:3608119为随意设定的7位数值
85为60-93的随机数值
10为01-12的随机数值
29为01-30的随机数值
5118为0001-9999的随机数值
// 下面不懂怎么改,高手看看
[解决办法]
#include <stdio.h>#include <stdlib.h>#include <time.h>void randstr(char str[], int s, int e, int n){ int num = rand() % (e - s + 1) + s; int i; for(i = n - 1; i >= 0; i--) { str[i] = '0' + num % 10; num /= 10; }}int main(int argc, char* argv[]){ char str[18]; srand(time(NULL)); randstr(str, 0000000, 9999999, 7); randstr(str + 7, 60, 93, 2); randstr(str + 9, 01, 12, 2); randstr(str + 11, 01, 30, 2); randstr(str + 13, 0001, 9999, 4); str[17] = '\0'; printf("%s\n", str); return 0;}