uses reservoir sampling - http://en.wikipedia.org/wiki/Reservoir_sampling (so it presumably consumes the entire stream before giving any results; any alternative i can think of would not be "really random" unless you knew the length of the stream in advance).
Yep, though a feature request I've put in is to respond to a ctl-c by producing the results from the stream so far... that way if it's taking a while on a large file you can interrupt and still get something useful.
Dimsum is like head or tail, but pulls randomly from the entire file or stream
11–20 of 31 posts
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#12uhhh. You mean like shuf -n NNNN ?
or "sort -R"...
(This bit me once. Filed a bug about maybe clarifying the man page, but nope, apparently we're all supposed to recognize instantly the implication of a random hash.)
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#13uses reservoir sampling - http://en.wikipedia.org/wiki/Reservoir_sampling (so it presumably consumes the entire stream before giving any results; any alternative i can think of would not be "really random" unless you knew the length of the stream in advance).
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#14uses reservoir sampling - http://en.wikipedia.org/wiki/Reservoir_sampling (so it presumably consumes the entire stream before giving any results; any alternative i can think of would not be "really random" unless you knew the length of the stream in advance).
Maybe I'm overlooking something obvious but wouldn't piping through awk 'rand()<.1{print}' give a decent streaming random sample of roughly 10% of input?
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#15Earlier quoted context omitted.
Yep, though a feature request I've put in is to respond to a ctl-c by producing the results from the stream so far... that way if it's taking a while on a large file you can interrupt and still get something useful.
This breaks ctl-c in my opinion. When I ctl-c I want shit to stop, not dump (potentially large quantities of) output into my terminal.
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#16https://github.com/neilk/misc/blob/master/randline
I've had this script (or versions of it) around for more than a decade. I didn't know the technique had a name.
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#17 $ seq 15 | dimsum -n 10
14
12
3
4
5
6
7
8
9
10Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#18Earlier quoted context omitted.
This breaks ctl-c in my opinion. When I ctl-c I want shit to stop, not dump (potentially large quantities of) output into my terminal.
It could accept another signal (e.g. SIGUSR1) and have this clearly documented.
e.g.
$ (while true; do cat /usr/share/dict/words; done;) | ./randline 3 &
[2] 93937
$ kill -s SIGUSR1 93937
declinograph
brotheler
woolpack
$ kill -s SIGUSR1 93937
lustrify
brotheler
bromophenolRe: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#19I really don't like how this behaves for populating the array initially, and how it behaves for small inputs... $ seq 15 | dimsum -n 10 14 12 3 4 5 6 7 8 9 10
Re: Dimsum is like head or tail, but pulls randomly from the entire file or stream
#20This doesn't come with an elaborate test suite, but it does pretty much everything that does in a few dozen lines of Perl. https://github.com/neilk/misc/blob/master/randline I've had this script (or versions of it) around for more than a decade. I didn't know the technique had a name.
[edit: i was going to delete this, but since you replied i'll leave it - it does appear (too?) in the camel book, on p 246 of my copy, but like you say, it's for a single line. hadn't opened that book in years, took me some time to find it...]