-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_str.c
50 lines (42 loc) · 902 Bytes
/
gen_str.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rand_int(int a, int b)
{
return a + rand() % (b-a+1);
}
char *rand_str(int len)
#define SIGMA \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"abcdefghijklmnopqrstuvwxyz" \
"0123456789"
{
char *str;
int j;
if ( (str=malloc(len+1)) == NULL )
{
printf( "rand_str--malloc\n" );
exit( 1 );
}
for ( j=0; j<len; ++j )
{
/* do not include NULL byte */
str[j] = SIGMA[rand_int(0, sizeof(SIGMA)-2)];
}
str[j] = '\0';
return str;
}
int main(int argc, char **argv)
{
srand( (long) clock() );
int j, k;
for ( j=0; j<10; ++j )
{
for ( k=0; k<5; ++k )
{
printf( "%10s ", rand_str(rand_int(5, 10)) );
}
printf("\n");
}
return 0;
}