at 23.11-beta 1.8 kB view raw
1# Generate random path-like strings, separated by null characters. 2# 3# Invocation: 4# 5# awk -f ./generate.awk -v <variable>=<value> | tr '\0' '\n' 6# 7# Customizable variables (all default to 0): 8# - seed: Deterministic random seed to use for generation 9# - count: Number of paths to generate 10# - extradotweight: Give extra weight to dots being generated 11# - extraslashweight: Give extra weight to slashes being generated 12# - extranullweight: Give extra weight to null being generated, making paths shorter 13BEGIN { 14 # Random seed, passed explicitly for reproducibility 15 srand(seed) 16 17 # Don't include special characters below 32 18 minascii = 32 19 # Don't include DEL at 128 20 maxascii = 127 21 upperascii = maxascii - minascii 22 23 # add extra weight for ., in addition to the one weight from the ascii range 24 upperdot = upperascii + extradotweight 25 26 # add extra weight for /, in addition to the one weight from the ascii range 27 upperslash = upperdot + extraslashweight 28 29 # add extra weight for null, indicating the end of the string 30 # Must be at least 1 to have strings end at all 31 total = upperslash + 1 + extranullweight 32 33 # new=1 indicates that it's a new string 34 new=1 35 while (count > 0) { 36 37 # Random integer between [0, total) 38 value = int(rand() * total) 39 40 if (value < upperascii) { 41 # Ascii range 42 printf("%c", value + minascii) 43 new=0 44 45 } else if (value < upperdot) { 46 # Dot range 47 printf "." 48 new=0 49 50 } else if (value < upperslash) { 51 # If it's the start of a new path, only generate a / in 10% of cases 52 # This is always an invalid subpath, which is not a very interesting case 53 if (new && rand() > 0.1) continue 54 printf "/" 55 56 } else { 57 # Do not generate empty strings 58 if (new) continue 59 printf "\x00" 60 count-- 61 new=1 62 } 63 } 64}