#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
void rand_str(char *, size_t);
int main(void) {
char str[42];
rand_str(str, sizeof str - 1);
assert(str
[41] == '\0'); // just to make sure I put the `'\0'` in the right place before I print it. }
void rand_str(char *dest, size_t length) {
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (length-- > 0) {
size_t index
= (double) rand() / RAND_MAX
* (sizeof charset
- 1); *dest++ = charset[index];
}
*dest = '\0';
}
ICAgICNpbmNsdWRlIDxhc3NlcnQuaD4KICAgICNpbmNsdWRlIDxzdGRpby5oPgogICAgI2luY2x1ZGUgPHN0ZGxpYi5oPgogICAgCiAgICB2b2lkIHJhbmRfc3RyKGNoYXIgKiwgc2l6ZV90KTsKICAgIAogICAgaW50IG1haW4odm9pZCkgewogICAgICAgIGNoYXIgc3RyWzQyXTsKICAgICAgICByYW5kX3N0cihzdHIsIHNpemVvZiBzdHIgLSAxKTsKICAgICAgICBhc3NlcnQoc3RyWzQxXSA9PSAnXDAnKTsgLy8ganVzdCB0byBtYWtlIHN1cmUgSSBwdXQgdGhlIGAnXDAnYCBpbiB0aGUgcmlnaHQgcGxhY2UgYmVmb3JlIEkgcHJpbnQgaXQuCiAgICAgICAgcHV0cyhzdHIpOwogICAgfQogICAgCiAgICB2b2lkIHJhbmRfc3RyKGNoYXIgKmRlc3QsIHNpemVfdCBsZW5ndGgpIHsKICAgICAgICBjaGFyIGNoYXJzZXRbXSA9ICIwMTIzNDU2Nzg5IgogICAgICAgICAgICAgICAgICAgICAgICAgImFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6IgogICAgICAgICAgICAgICAgICAgICAgICAgIkFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaIjsKCiAgICAgICAgd2hpbGUgKGxlbmd0aC0tID4gMCkgewogICAgICAgICAgICBzaXplX3QgaW5kZXggPSAoZG91YmxlKSByYW5kKCkgLyBSQU5EX01BWCAqIChzaXplb2YgY2hhcnNldCAtIDEpOwogICAgICAgICAgICAqZGVzdCsrID0gY2hhcnNldFtpbmRleF07CiAgICAgICAgfQogICAgICAgICpkZXN0ID0gJ1wwJzsKICAgIH0=