linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Random number generator in Linux kernel
@ 2005-03-07 23:18 Vineet Joglekar
  2005-03-08  0:25 ` J.
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Vineet Joglekar @ 2005-03-07 23:18 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-c-programming


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

_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Random number generator in Linux kernel
  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
  2005-03-08 12:59 ` Darío Mariani
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: J. @ 2005-03-08  0:25 UTC (permalink / raw)
  To: linux-c-programming

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/


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Random number generator in Linux kernel
  2005-03-08  0:25 ` J.
@ 2005-03-08  4:40   ` Anindya Mozumdar
  0 siblings, 0 replies; 6+ messages in thread
From: Anindya Mozumdar @ 2005-03-08  4:40 UTC (permalink / raw)
  To: linux-c-programming; +Cc: vintya, mailing-lists

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Random number generator in Linux kernel
  2005-03-07 23:18 Random number generator in Linux kernel Vineet Joglekar
  2005-03-08  0:25 ` J.
@ 2005-03-08 12:59 ` Darío Mariani
  2005-03-08 13:31 ` Erik Mouw
  2005-03-09 20:39 ` Bill Davidsen
  3 siblings, 0 replies; 6+ messages in thread
From: Darío Mariani @ 2005-03-08 12:59 UTC (permalink / raw)
  Cc: linux-kernel, linux-c-programming

As far as I understand the kernel generates random numbers gathering
data from several entropy sources, you will never get repetability
from there. Two options I know of:

1) The standard C library has the functions rand and random, wich
seems to have a decent distribution of the random numbers.

2) If you use C++, the Boost library (www.boost.org) has an excelent
set of options for generating random numbers.

              Darío

On Mon,  7 Mar 2005 18:18:53 -0500 (EST), Vineet Joglekar
<vintya@excite.com> 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 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.
> 
> _______________________________________________
> Join Excite! - http://www.excite.com
> The most personalized portal on the Web!
> -
> 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
> 


-- 
That is not dead which can eternal lie,
and with strange aeons, even death may die.
-
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Random number generator in Linux kernel
  2005-03-07 23:18 Random number generator in Linux kernel Vineet Joglekar
  2005-03-08  0:25 ` J.
  2005-03-08 12:59 ` Darío Mariani
@ 2005-03-08 13:31 ` Erik Mouw
  2005-03-09 20:39 ` Bill Davidsen
  3 siblings, 0 replies; 6+ messages in thread
From: Erik Mouw @ 2005-03-08 13:31 UTC (permalink / raw)
  To: Vineet Joglekar; +Cc: linux-kernel, linux-c-programming

On Mon, Mar 07, 2005 at 06:18:53PM -0500, Vineet Joglekar wrote:
> 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.

Google for "Numerical recipes in C", it has a complete section about
random numbers, including a couple of functions that do what you want.


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Random number generator in Linux kernel
  2005-03-07 23:18 Random number generator in Linux kernel Vineet Joglekar
                   ` (2 preceding siblings ...)
  2005-03-08 13:31 ` Erik Mouw
@ 2005-03-09 20:39 ` Bill Davidsen
  3 siblings, 0 replies; 6+ messages in thread
From: Bill Davidsen @ 2005-03-09 20:39 UTC (permalink / raw)
  To: vintya; +Cc: linux-kernel, linux-c-programming

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

Without knowing what you're doing I can't say if it justifies having all 
that extra code around, but the stuff from the library, like srand48, 
will do this. You can add the code to your module.

-- 
    -bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-03-09 20:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2005-03-08 12:59 ` Darío Mariani
2005-03-08 13:31 ` Erik Mouw
2005-03-09 20:39 ` Bill Davidsen

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