coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generating pseudo-random integers


From: Jim Meyering
Subject: Re: Generating pseudo-random integers
Date: Sat, 05 Feb 2011 08:51:14 +0100

Melikamp T. Medley wrote:

> Thanks!
>
> I think I want to write a utility that prints pseudo-random
> integers (I have in CL, but I like it fast, so this time in C),
> and link it with coreutils. I looked at Paul Eggert's work,
> and I won't be able to do any better than that if I spend
> years on it :) I want to print uniformly distributed integers
> fast, formatted, in bulk if needed. And I want to call it roll.
> Let me know if you have a word of advice.
>
> On 02/04/2011 11:35 PM, Eric Blake wrote:
>> Using just coreutils, though, I'm afraid your options are slim.  You
>> could use 'seq 0 9 | sort -R | head -n1' (or use shuf instead of sort
>> -R) to generate one random number at a time, but that gets expensive.
>> Or, if you don't mind limiting yourself to 62**6 values (instead of the
>> more traditional 2**32), you could do 'mkdir tmp; mktemp -u -p tmp
>> XXXXXX'.  But that's the extent of coreutils' randomness exposed to the
>> user.

It's hard to justify writing a new program in C
when you can do the job with a Perl one-liner:

  $ perl -le 'foreach (1..20) { print int rand 1000 }'
  423
  619
  917
  396
  957
  345
  892
  13
  424
  519
  898
  689
  208
  383
  605
  196
  384
  893
  618
  344

However, adding an *option* to shuf might make sense,
since you can already give shuf both a range and a count
of how many numbers to print.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]