mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-24 08:28:18 +00:00
82 lines
2.3 KiB
Markdown
82 lines
2.3 KiB
Markdown
# Randomizing examples
|
|
|
|
## Generating random numbers from various distributions
|
|
|
|
Here we can chain together a few simple building blocks:
|
|
|
|
GENMD-RUN-COMMAND
|
|
cat expo-sample.sh
|
|
GENMD-EOF
|
|
|
|
Namely:
|
|
|
|
* Set the Miller random-number seed so this webdoc looks the same every time I regenerate it.
|
|
* Use pretty-printed tabular output.
|
|
* Use `seqgen` to produce 100,000 records `i=0`, `i=1`, etc.
|
|
* Send those to a `put` step which defines an inverse-transform-sampling function and calls it twice, then computes the sum and product of samples.
|
|
* Send those to a histogram, and from there to a bar-plotter. This is just for visualization; you could just as well output CSV and send that off to your own plotting tool, etc.
|
|
|
|
The output is as follows:
|
|
|
|
GENMD-RUN-COMMAND
|
|
sh expo-sample.sh
|
|
GENMD-EOF
|
|
|
|
## Randomly selecting words from a list
|
|
|
|
Given this [word list](./data/english-words.txt), first take a look to see what the first few lines look like:
|
|
|
|
GENMD-CARDIFY-HIGHLIGHT-ONE
|
|
head data/english-words.txt
|
|
a
|
|
aa
|
|
aal
|
|
aalii
|
|
aam
|
|
aardvark
|
|
aardwolf
|
|
aba
|
|
abac
|
|
abaca
|
|
GENMD-EOF
|
|
|
|
Then the following will randomly sample ten words with four to eight characters in them:
|
|
|
|
GENMD-CARDIFY-HIGHLIGHT-ONE
|
|
mlr --from data/english-words.txt --nidx filter -S 'n=strlen($1);4<=n&&n<=8' then sample -k 10
|
|
thionine
|
|
birchman
|
|
mildewy
|
|
avigate
|
|
addedly
|
|
abaze
|
|
askant
|
|
aiming
|
|
insulant
|
|
coinmate
|
|
GENMD-EOF
|
|
|
|
## Randomly generating jabberwocky words
|
|
|
|
These are simple *n*-grams, adapted from a previous version [described here](http://johnkerl.org/randspell/randspell-slides-ts.pdf). Some common functions are [located here](https://github.com/johnkerl/miller/blob/master/docs/src/ngrams/ngfuncs.mlr) with main Miller script [here](https://github.com/johnkerl/miller/blob/master/docs/src/ngrams/ngrams.mlr) and wrapper script [here](https://github.com/johnkerl/miller/blob/master/docs/src/ngrams/ngrams.sh).
|
|
|
|
The idea is that words from the input file are consumed, then taken apart and pasted back together in ways which imitate the letter-to-letter transitions found in the word list -- giving us automatically generated words in the same vein as *bromance* and *spork*:
|
|
|
|
GENMD-CARDIFY-HIGHLIGHT-ONE
|
|
mlr --nidx --from ./ngrams/gsl-2000.txt put -q -f ./ngrams/ngfuncs.mlr -f ./ngrams/ngrams.mlr
|
|
burse
|
|
serious
|
|
land
|
|
seasure
|
|
clainst
|
|
tray
|
|
wherhoose
|
|
stry
|
|
jourt
|
|
strue
|
|
partist
|
|
ornear
|
|
devel
|
|
praction
|
|
roup
|
|
GENMD-EOF
|