[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
- Generating pseudo-random integers, Melikamp T. Medley, 2011/02/04
- Re: Generating pseudo-random integers, Eric Blake, 2011/02/04
- Re: Generating pseudo-random integers, Melikamp T. Medley, 2011/02/05
- Re: Generating pseudo-random integers,
Bob Proulx <=
- Re: Generating pseudo-random integers, Melikamp The Medley, 2011/02/05
- Re: Generating pseudo-random integers, Bob Proulx, 2011/02/05
- Message not available
- Message not available
- Re: Generating pseudo-random integers, Melikamp The Medley, 2011/02/05
- Re: Generating pseudo-random integers, Bob Proulx, 2011/02/05
- Re: Generating pseudo-random integers, Melikamp T. Medley, 2011/02/07
- Re: Generating pseudo-random integers, Jim Meyering, 2011/02/05
- Re: Generating pseudo-random integers, Pádraig Brady, 2011/02/05
- Re: Generating pseudo-random integers, Jim Meyering, 2011/02/05