Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <time.h>
- char* rand_str(const int length) {
- const int n_len = 1 + length / sizeof(uint32_t);
- uint32_t * const n = malloc(n_len);
- const uint32_t mask_printable = 0x20202020; // make sure it's printable
- const uint32_t mask_ascii = 0x7F7F7F7F; // keep within ASCII range
- // The shift is endianess-dependent. Right shift needed on little-endian.
- const uint32_t last_mask = length % 4 ? mask_ascii >> 8*(4 - (length % 4)) : 0;
- for(int i = 0; i < n_len; i++) {
- n[i] = (uint32_t) ((int64_t) rand() - INT32_MIN);
- n[i] |= mask_printable;
- n[i] &= i == n_len - 1 ? last_mask : mask_ascii;
- }
- return (char*) n;
- }
- int main(int argc, char **argv) {
- srand(time(NULL));
- int len = argc > 1 ? atoi(argv[1]) : 7;
- int count = argc > 2 ? atoi(argv[2]) : 1;
- char *s;
- for(int i = 0; i < count; i++) {
- s = rand_str(len);
- printf("%s\n", s);
- free(s);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment