feature request: sleep --random

2009-09-18 Thread Philip Rowlands
This would help with some work I'm doing today, but is it of general 
interest?


$ sleep --random 4.0

sleeps for a random amount of time up to and including the requested 
value. The purpose is that on distributed systems it's disruptive to 
have synchronized scripts all starting up together. One option is to use 
the shell's $RANDOM if available, but it's lot of typing to get true (vs 
coarsely quantized) randomness.



Phil




Re: feature request: sleep --random

2009-09-18 Thread Pádraig Brady
Philip Rowlands wrote:
 This would help with some work I'm doing today, but is it of general
 interest?
 
 $ sleep --random 4.0
 
 sleeps for a random amount of time up to and including the requested
 value. The purpose is that on distributed systems it's disruptive to
 have synchronized scripts all starting up together. One option is to use
 the shell's $RANDOM if available, but it's lot of typing to get true (vs
 coarsely quantized) randomness.

Be careful with random sleeps:
http://www.stdlib.net/~colmmacc/2009/09/14/period-pain/

You're right that the existing is more verbose
but it's also more general:
sleep $(seq .1 .1 4 | head -n $(($RANDOM%40 +1)) | tail -n1)

I'm not sure we should add this.

cheers,
Pádraig.




Re: feature request: sleep --random

2009-09-18 Thread Jim Meyering
Philip Rowlands wrote:
 This would help with some work I'm doing today, but is it of general
 interest?

 $ sleep --random 4.0

 sleeps for a random amount of time up to and including the requested
 value. The purpose is that on distributed systems it's disruptive to
 have synchronized scripts all starting up together. One option is to
 use the shell's $RANDOM if available, but it's lot of typing to get
 true (vs coarsely quantized) randomness.

How about this:

  sleep $(perl -le 'print rand 4')

That should work with more shells, and is smooth.




Re: feature request: sleep --random

2009-09-18 Thread Pádraig Brady
Pádraig Brady wrote:
 sleep $(seq .1 .1 4 | head -n $(($RANDOM%40 +1)) | tail -n1)

Or more concisely using just coreutils logic:

sleep $(seq .1 .1 4 | shuf | head -n1)

cheers,
Pádraig.




Re: feature request: sleep --random

2009-09-18 Thread Philip Rowlands

On Fri, 18 Sep 2009, Pádraig Brady wrote:


Pádraig Brady wrote:

sleep $(seq .1 .1 4 | head -n $(($RANDOM%40 +1)) | tail -n1)


Or more concisely using just coreutils logic:

sleep $(seq .1 .1 4 | shuf | head -n1)


This still has the quantization effects which I'm trying to avoid. Jim's 
perl suggestion would work well to ensure an even spread over the window 
of time.


$ perl -e 'sleep rand 4'

is probably the cleanest / most efficient way to do this with existing 
tools.



Cheers,
Phil




Re: feature request: sleep --random

2009-09-18 Thread Pádraig Brady
Philip Rowlands wrote:
 On Fri, 18 Sep 2009, Pádraig Brady wrote:
 
 Pádraig Brady wrote:
 sleep $(seq .1 .1 4 | head -n $(($RANDOM%40 +1)) | tail -n1)

 Or more concisely using just coreutils logic:

 sleep $(seq .1 .1 4 | shuf | head -n1)
 
 This still has the quantization effects which I'm trying to avoid. Jim's
 perl suggestion would work well to ensure an even spread over the window
 of time.
 
 $ perl -e 'sleep rand 4'
 
 is probably the cleanest / most efficient way to do this with existing
 tools.

right.

Note the scheduler is still going to quantize your sleep values,
so in practicality your probably not going to get smoother than:

  sleep $(seq .002 .002 4 | shuf | head -n1)

cheers,
Pádraig.

p.s. That's another use case for sort --range




Re: feature request: sleep --random

2009-09-18 Thread Jim Meyering
Pádraig Brady wrote:
 Pádraig Brady wrote:
 sleep $(seq .1 .1 4 | head -n $(($RANDOM%40 +1)) | tail -n1)

 Or more concisely using just coreutils logic:

 sleep $(seq .1 .1 4 | shuf | head -n1)

Or save a pipe+process:

sleep $(seq .1 .1 4 | shuf --head=1)