From: Steve Graegert <graegerts@gmail.com>
To: HIToC <hitoc_mail@yahoo.it>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: How to use /dev/random
Date: Thu, 22 Sep 2005 11:53:43 +0200 [thread overview]
Message-ID: <6a00c8d5050922025348a449a5@mail.gmail.com> (raw)
In-Reply-To: <200509221111.07711.hitoc_mail@yahoo.it>
On 9/22/05, HIToC <hitoc_mail@yahoo.it> wrote:
> Hello list!
> Is there anyone who can explain how to use /dev/random in
> order to obtain random numbers?
There are two types:
1. /dev/random generates high quality entropy. It's relatively slow
since it uses other information like mouse clicks, key strokes and
interrupt times to produce random numbers. For this reason, some
applications can block for some time when reading from /dev/random.
2. /dev/urandom is much faster with lower quality sequences of random
numbers but it should be sufficient for most applications.
> I don't know if this is the best manner whitin the Linux Kernel to
> generate random numbers, but if someone has better ideas, they
> will be greatly appreciated.
You can read from /dev/(u)random using the read system call:
int randnum, fd;
if ((fd = open ("/dev/urandom", O_RDONLY)) != -1) {
read(fd, &randnum, 4 );
/* call abs() to produces positives */
printf("random number read : %d\n",randnum);
close(fd);
}
You can also read a number of random bytes from /dev/(u)random with dd:
dd if=/dev/urandom of=/tmp/randnums bs=1 count=500
This reads a sequence of 500 bytes from /dev/urandom and writes them
to /tmp/randnums.
Regards
\Steve
--
Steve Graegert <graegerts@gmail.com>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176) 21248869
Office: +49 (9131) 7126409
next prev parent reply other threads:[~2005-09-22 9:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-22 9:11 How to use /dev/random HIToC
2005-09-22 9:42 ` Hagen Paul Pfeifer
2005-09-22 9:53 ` Steve Graegert [this message]
2005-09-23 9:16 ` HIToC
2005-09-23 15:44 ` Glynn Clements
2005-09-23 16:31 ` HIToC
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6a00c8d5050922025348a449a5@mail.gmail.com \
--to=graegerts@gmail.com \
--cc=hitoc_mail@yahoo.it \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).