--- /sys/src/cmd/random.c Mon Nov 7 11:12:33 2016 +++ /sys/src/cmd/random.c Mon Nov 7 00:00:00 2016 @@ -0,0 +1,53 @@ +/* + * random - print a random number or string + */ + +#include +#include +#include +#include + +static int string, prchars, low, high; + +static void +usage(void) +{ + fprint(2, "usage: %s [-s nch]\n", argv0); + exits("usage"); +} + +void +main(int argc, char *argv[]) +{ + char *buf, *out; + + ARGBEGIN { + case 's': + ++string; + prchars = atoi(EARGF(usage())); + break; + default: + usage(); + break; + } ARGEND + + if (!string) { + print("%f\n", (double)ntruerand(~0ul) / (1ull<<32)); + exits(0); + } + + /* fill buf from /dev/random */ + buf = malloc(prchars + 1); + if (buf == nil) + sysfatal("out of memory: %r"); + genrandom((uchar *)buf, prchars); + buf[prchars] = '\0'; + + /* encode as printable (base64) and print truncated */ + fmtinstall('[', encodefmt); + out = smprint("%.*[", prchars, buf); + if (out == nil) + sysfatal("out of memory: %r"); + print("%.*s\n", prchars, out); + exits(0); +} --- /sys/man/1/random Mon Nov 7 11:12:34 2016 +++ /sys/man/1/random Mon Nov 7 00:00:00 2016 @@ -0,0 +1,24 @@ +.TH RANDOM 1 +.SH NAME +random - print truly random probability or string +.SH SYNOPSIS +.B random +[ +.B -s +chars +] +.SH DESCRIPTION +.I Random +normally prints a truly random number between 0 and 1. +Given +.BR -s , +it instead prints a base-64-encoded truly random string of +.I chars +characters. +.SH SOURCE +.B /sys/src/cmd/random.c +.SH SEE ALSO +.IR rand (2), +.B /dev/random +in +.IR cons (3)