linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anindya Mozumdar <anindya@cmi.ac.in>
To: linux-c-programming@vger.kernel.org
Cc: vintya@excite.com, mailing-lists@xs4all.nl
Subject: Re: Random number generator in Linux kernel
Date: Tue, 8 Mar 2005 10:10:42 +0530	[thread overview]
Message-ID: <20050308044042.GA9364@cmi.ac.in> (raw)
In-Reply-To: <Pine.LNX.4.21.0503080109090.1705-100000@hestia>

Hi,
   This method ,i.e, using /dev/urandom should work but there should be 
   sufficient entropy to generate enough random data. For example, if
   you take an octal dump of /dev/urandom (hexdump quits when there is
   no more data in the file while od waits for more data), and then dont
   touch your machine for a few seconds, it stops - and starts printing
   the dump only when you move the mouse or something.
   Sorry for being very vague, but I guess the idea should be clear.
Anindya.

On Tue, Mar 08, 2005 at 01:25:23AM +0100, J. wrote:
> On Mon, 7 Mar 2005, Vineet Joglekar wrote:
> > Hi all,
> > 
> > Can someone please tell me where can I find and which random/pseudo-random 
> > number generator can I use inside the linux kernel? (2.4.28)
> 
> I Don't know how this is done for actual use in the kernel itself - So I
> cannot answer that particular question and... I may be way out of my
> league here, but why not use /dev/urandom ? Just to illustrate! I have
> included an example of that below... If it lives up to your idea of
> `cryptographicly secure' - ... ? 
> 
> > I found out 1 function get_random_bytes() in linux/drivers/char/random.c 
> > but thats not what I want.
> > 
> > I want a function where I will be supplying a seed to that function as an input, 
> > and will get a random number back. If same seed is used, same number
> > should be generated again.
> > 
> > Can anybody please help me with that?
> > 
> > Thanks and regards,
> > 
> > Vineet.
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <time.h>
> 
> unsigned time_seed(void);
> int get_rand_val(int low, int high);
> 
> int main(void) {
>  int i = 0;
> 
>  srand((time_seed()));
> 
>  for(i = 0; i < 10; i++)
>   printf("%d\n", get_rand_val(1, 10));
> 
>  return 0;
> }
> 
> int get_rand_val(int low, int high) {
>  int k = 0;
>  double d = 0;
> 
>  d = (double)rand() / ((double)RAND_MAX + 1);
>  k = (int)(d * (high - low + 1));
>  return(low + k);
> }
> 
> unsigned time_seed(void) {
>  int retval = 0;
>  int fd = 0;
>     
>  if(open("/dev/urandom", O_RDONLY) == -1) {
>   retval = (((int)time(NULL)) & ((1 << 30) - 1)) + getpid();
>  } else {
>   read(fd, &retval, 4);
>   /* positive values only */
>   retval = abs(retval) + getpid();
>   close(fd);
>  }
> 
>  return retval;
> }
> 
> Cheers, J.
> 
> --
> http://www.rdrs.net/
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2005-03-08  4:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-07 23:18 Random number generator in Linux kernel Vineet Joglekar
2005-03-08  0:25 ` J.
2005-03-08  4:40   ` Anindya Mozumdar [this message]
2005-03-08 12:59 ` Darío Mariani
2005-03-08 13:31 ` Erik Mouw
2005-03-09 20:39 ` Bill Davidsen

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=20050308044042.GA9364@cmi.ac.in \
    --to=anindya@cmi.ac.in \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=mailing-lists@xs4all.nl \
    --cc=vintya@excite.com \
    /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).