8
[C] Generate Random String From [a-z0-9]
source link: http://siongui.github.io/2017/02/09/c-generate-random-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
[C] Generate Random String From [a-z0-9]
February 09, 2017
Generate a random string from [a-z0-9] in C programming language.
ranstr.c | repository | view raw
#include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* alphabet: [a-z0-9] */ const char alphabet[] = "abcdefghijklmnopqrstuvwxyz0123456789"; /** * not a cryptographically secure number * return interger [0, n). */ int intN(int n) { return rand() % n; } /** * Input: length of the random string [a-z0-9] to be generated */ char *randomString(int len) { char *rstr = malloc((len + 1) * sizeof(char)); int i; for (i = 0; i < len; i++) { rstr[i] = alphabet[intN(strlen(alphabet))]; } rstr[len] = '\0'; return rstr; } int main(int argc, char **argv) { // the seed for a new sequence of pseudo-random integers // to be returned by rand() srand(time(NULL)); char *p; p = randomString(10); printf("%s\n", p); free(p); p = randomString(11); printf("%s\n", p); free(p); p = randomString(12); printf("%s\n", p); free(p); }
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK