秒作爲隨機數,可能會重複
採用毫秒級別
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 10
void get_rand(char *p, int length) {
int i;
unsigned int seed;
char value[] = "0123456789abcdef";
struct timeval cur_t;
gettimeofday(&cur_t, NULL);
printf("sec:%d uc:%d\n", cur_t.tv_sec, cur_t.tv_usec);
seed = cur_t.tv_sec*1000 + cur_t.tv_usec;
// srand(time(NULL));
srand(seed);
for ( i = 0; i < length; ++i)
{
*(p + i) = value[rand() % 16];
}
*(p + length) = '\0';
return;
}
int main()
{
unsigned char value[SIZE+1] = {0};
get_rand(value, SIZE);
printf("value is: %s, %d\n", value, value[SIZE]);
return 0;
}