1. 程式人生 > >C語言之獲取32位元組隨機數的字串

C語言之獲取32位元組隨機數的字串

1、問題

獲取32位元組隨機數的字串

2、程式碼實現

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 32

void get_rand(char *p,  int length) {	
	char value[10] = "0123456789";
	srand(time(NULL));
	for (int i = 0; i < length; ++i) {
		*(p + i) = value[rand() % 10];
		count++;
	}
	*(p + SIZE) = '\0';
	return;
}

int main() {
	unsigned char value[SIZE] = {0}; 
	printf("before value is: %s\n, length is %d\n", value, strlen(value));
	get_rand(value, SIZE);
	printf("before value is: %s\n, length is %d\n", value, strlen(value));
}