fork(1) download
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void rand_str(char *, size_t);
  6.  
  7. int main(void) {
  8. char str[42];
  9. rand_str(str, sizeof str - 1);
  10. assert(str[41] == '\0'); // just to make sure I put the `'\0'` in the right place before I print it.
  11. puts(str);
  12. }
  13.  
  14. void rand_str(char *dest, size_t length) {
  15. char charset[] = "0123456789"
  16. "abcdefghijklmnopqrstuvwxyz"
  17. "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  18.  
  19. while (length-- > 0) {
  20. size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
  21. *dest++ = charset[index];
  22. }
  23. *dest = '\0';
  24. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
QoMNUckLhytCmvXUDI8B1f8N9o86ZdvQBiDwuYiLw