Created
February 4, 2016 14:50
-
-
Save svagionitis/82cda46df0ec56b8e89f to your computer and use it in GitHub Desktop.
Generate a random string which will be used as a UUID.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: rand_uuid | |
rand_uuid: rand_uuid.c | |
gcc -o rand_uuid -O2 -Wall -W -ansi -pedantic -std=gnu99 rand_uuid.c | |
clean: | |
rm -rf rand_uuid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <time.h> | |
// Generate a random string which will be used | |
// as a UUID. | |
static char *_get_uuid(char *uuid, size_t uuid_sz) | |
{ | |
const char charset[] = "0123456789abcdefghijklmnopqrstuvwxyz"; | |
size_t charset_len = strlen(charset); | |
if (uuid && uuid_sz) | |
{ | |
for (size_t i = 0;i < uuid_sz;i++) | |
{ | |
size_t rand_key = rand() % charset_len; | |
uuid[i] = charset[rand_key]; | |
} | |
uuid[uuid_sz] = '\0'; | |
} | |
return uuid; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char uuid[33]; | |
srand(time(NULL)); | |
strncpy (uuid, _get_uuid(uuid, sizeof(uuid)), sizeof(uuid)); | |
fprintf(stdout, "Rand UUID: %s\n", uuid); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment