【发布时间】:2023-03-17 14:35:02
【问题描述】:
我在函数中生成随机字符串时遇到问题。
在下面的代码中,我使用了 65 到 90 的 ASCII 字符。我想包括 48 到 57,跳过 58 到 64。
有什么办法吗?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main()
{
char s[30];
random_string(s, 6,65,90);
printf("\n%s\n", s);
return 0;
}
void random_string(char * string, unsigned length,int min,int max)
{
/* Seed number for rand() */
srand((unsigned int) time(0) + getpid());
/* ASCII characters 33 to 126 */
unsigned int num_chars = length - 1;
unsigned int i;
for (i = 0; i < num_chars; ++i)
{
string[i] = rand() % (max - min + 1) + min;
}
string[num_chars] = '\0';
}
【问题讨论】: