coreutils
[Top][All Lists]
Advanced

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

Re: Generating pseudo-random integers


From: Bob Proulx
Subject: Re: Generating pseudo-random integers
Date: Sat, 5 Feb 2011 00:20:14 -0700
User-agent: Mutt/1.5.20 (2009-06-14)

Melikamp T. Medley wrote:
> 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),

I am confused by the connection of using the shell on one hand and by
saying you need speed on the other.  The shell is quite fast and good
enough for most things but it isn't the ultimate speed daemon.

> and link it with coreutils.

Why?

> 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.

Before you get too far along you should benchmark it against using awk
which already exists and is standard.  Please also state your
rationale for needing something different.

Want a random number between 0 and 9?

  awk 'BEGIN{srand();print int(10*rand());}'

Want a thousand random numbers between 0 and 99?

  awk 'BEGIN{srand();for(i=0;i<1000;++i){print int(100*rand());}}'

Timing this on my system:

  time awk 'BEGIN{srand();for(i=0;i<1000;++i){print int(100*rand());}}' > 
/dev/null
  real    0m0.005s
  user    0m0.004s
  sys     0m0.004s

That is pretty fast!

If you are running on a Linux kernel or others that have followed and
can accept the portability issues then reading from /dev/random and
/dev/urandom are typical.

Do we really need yet another random number generator?  Random number
generators are by their very nature targets of attack.

Bob



reply via email to

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