Skip to content

Instantly share code, notes, and snippets.

@svagionitis
Created February 4, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svagionitis/82cda46df0ec56b8e89f to your computer and use it in GitHub Desktop.
Save svagionitis/82cda46df0ec56b8e89f to your computer and use it in GitHub Desktop.
Generate a random string which will be used as a UUID.
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
#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