Set of scripts used for various projects.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

45 lines
758 B

  1. #!/bin/sh -
  2. # Run time: O(number of lines)
  3. # Memory: O(longest line)
  4. # srand seed:
  5. # "dd if=/dev/random 2>/dev/null | LC_ALL=C tr -d -c 1-9 | dd bs=1 count=19 2>/dev/null" | getline seed; srand(seed)
  6. # + rand()
  7. LC_ALL=C awk '
  8. function urandom(n) {
  9. # return random [0, n)
  10. # maxn needs to be 2**(x*4), and cbs=x
  11. maxn = 4294967296
  12. if (n == 1)
  13. return 0
  14. if (n > maxn) {
  15. printf("n too large!\n") > "/dev/stderr"
  16. exit(5)
  17. }
  18. for (;;) {
  19. "dd if=/dev/random | LC_ALL=C tr -c -d 0-9a-f | dd conv=unblock cbs=8" | getline a
  20. r = ("0x" a) + 0
  21. quot = int(maxn / n) * n
  22. if (r >= quot)
  23. continue
  24. return r % n
  25. }
  26. }
  27. {
  28. #"jot -r 1 0 " (NR - 1) | getline a
  29. a = urandom(NR)
  30. #a = int(rand() * (NR))
  31. if (a == 0)
  32. save = $0
  33. }
  34. END { print save }'