All of lore.kernel.org
 help / color / mirror / Atom feed
* Creating the same random numbers
@ 2011-01-29 14:09 Andreas Leppert
  2011-01-29 19:34 ` Anuz Pratap Singh Tomar
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Leppert @ 2011-01-29 14:09 UTC (permalink / raw)
  To: kernelnewbies

Hi,

for my evaluation of different kernel modules which rely on randomness I
need the same set of random numbers for each of them. 

Is there any chance that I can get a random number which depends on a
kind of seed value? So if I start with the same seed value, the same
random numbers should be generated. 

Is there any way in the kernel to achieve this?

Thanks for your hints,
Andreas Leppert

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

* Creating the same random numbers
  2011-01-29 14:09 Creating the same random numbers Andreas Leppert
@ 2011-01-29 19:34 ` Anuz Pratap Singh Tomar
  2011-01-29 21:09   ` Enrico Granata
                     ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Anuz Pratap Singh Tomar @ 2011-01-29 19:34 UTC (permalink / raw)
  To: kernelnewbies

On Sat, Jan 29, 2011 at 2:09 PM, Andreas Leppert <wudmx@web.de> wrote:

> Hi,
>
> for my evaluation of different kernel modules which rely on randomness I
> need the same set of random numbers for each of them.
>
> Is there any chance that I can get a random number which depends on a
> kind of seed value? So if I start with the same seed value, the same
> random numbers should be generated.
>
> If the same numbers are generated, then how can you call it random?


> Is there any way in the kernel to achieve this?
>
> Thanks for your hints,
> Andreas Leppert
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110129/e45a98fd/attachment.html 

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

* Creating the same random numbers
  2011-01-29 19:34 ` Anuz Pratap Singh Tomar
@ 2011-01-29 21:09   ` Enrico Granata
  2011-01-29 23:38   ` Andreas Leppert
       [not found]   ` <1041152607-1296331182-cardhu_decombobulator_blackberry.rim.net-897203629-@b27.c7.bise7.blackberry>
  2 siblings, 0 replies; 8+ messages in thread
From: Enrico Granata @ 2011-01-29 21:09 UTC (permalink / raw)
  To: kernelnewbies

Apart from the obvious consideration that repeating numbers are NOT random anymore, I guess what the original question asker wanted to know is:

I need to generate some random numbers (so I need a PRNG). Then, I need to save these random numbers somewhere so I can use them over and over again to check some kernel modules.
Can't you simply write a C program that generates, say, 1 million random values, writes them to a file, and then use them?

Enrico Granata
Computer Science & Engineering Department (EBU3B) - Room 3240
office phone 858 534 9914
University of California, San Diego

On Jan 29, 2011, at 11:34 AM, Anuz Pratap Singh Tomar wrote:

> 
> 
> On Sat, Jan 29, 2011 at 2:09 PM, Andreas Leppert <wudmx@web.de> wrote:
> Hi,
> 
> for my evaluation of different kernel modules which rely on randomness I
> need the same set of random numbers for each of them.
> 
> Is there any chance that I can get a random number which depends on a
> kind of seed value? So if I start with the same seed value, the same
> random numbers should be generated.
> 
> If the same numbers are generated, then how can you call it random?
>  
> Is there any way in the kernel to achieve this?
> 
> Thanks for your hints,
> Andreas Leppert
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110129/b131c7ab/attachment.html 

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

* Creating the same random numbers
  2011-01-29 19:34 ` Anuz Pratap Singh Tomar
  2011-01-29 21:09   ` Enrico Granata
@ 2011-01-29 23:38   ` Andreas Leppert
       [not found]   ` <1041152607-1296331182-cardhu_decombobulator_blackberry.rim.net-897203629-@b27.c7.bise7.blackberry>
  2 siblings, 0 replies; 8+ messages in thread
From: Andreas Leppert @ 2011-01-29 23:38 UTC (permalink / raw)
  To: kernelnewbies

Hi,

On Sat, Jan 29, 2011 at 07:34:21PM +0000, Anuz Pratap Singh Tomar wrote:
> > for my evaluation of different kernel modules which rely on randomness I
> > need the same set of random numbers for each of them.
> >
> > Is there any chance that I can get a random number which depends on a
> > kind of seed value? So if I start with the same seed value, the same
> > random numbers should be generated.
> >
> If the same numbers are generated, then how can you call it random?
 
I want to compare different kernel modules which all require some data
based on randomness. But I can only compare them correctly, if they get
the same random data at a certain stage. 

I do only need this (the "fixed random numbers") for my evaluation!

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

* Creating the same random numbers
       [not found]   ` <1041152607-1296331182-cardhu_decombobulator_blackberry.rim.net-897203629-@b27.c7.bise7.blackberry>
@ 2011-01-29 23:41     ` Andreas Leppert
  2011-01-30  0:11       ` Manohar Vanga
  2011-01-29 23:49     ` Andreas Leppert
  1 sibling, 1 reply; 8+ messages in thread
From: Andreas Leppert @ 2011-01-29 23:41 UTC (permalink / raw)
  To: kernelnewbies

Hi Danny,


On Sat, Jan 29, 2011 at 07:59:34PM +0000, creslin at gmail.com wrote:
> Why not simply copy the sequence generated if you need the same? 
> 
Because it would be easier to have something like srand(seed) and later
on rand() instead of managing a list of generated numbers etc. Of
course, providing a list of generated data could be a solution.

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

* Creating the same random numbers
       [not found]   ` <1041152607-1296331182-cardhu_decombobulator_blackberry.rim.net-897203629-@b27.c7.bise7.blackberry>
  2011-01-29 23:41     ` Andreas Leppert
@ 2011-01-29 23:49     ` Andreas Leppert
  2011-01-30  0:34       ` Javier Martinez Canillas
  1 sibling, 1 reply; 8+ messages in thread
From: Andreas Leppert @ 2011-01-29 23:49 UTC (permalink / raw)
  To: kernelnewbies

On Sat, Jan 29, 2011 at 07:59:34PM +0000, creslin at gmail.com wrote:
> But in short answer yes, if you use the same seed the same answer
> would be generated, assuming no further entropy is used later I.e
> dependent on method employed to expand. Though then the 'randomness'
> is in the seed not the output. Its this capability which allows simple
> access control token devices to operate.
> 
I'm not quite familiar with the number generators in the kernel. Right
now I do this to get a random number betwen min and max:

/* compute the random value within the given min and max wrt to step */
int tmp_bytes, range;

range = max - min + 1;
get_random_bytes(&tmp_bytes, sizeof(tmp_bytes));
if (0 > tmp_bytes)
	tmp_bytes = -tmp_bytes;
new_random_param->curval = tmp_bytes % range + min;

You mentioned the seed value. Is there a way to set a seed value before
I invoke get_random_bytes? Could you direct me to a page / source file
where I can get more information about it?

Thanks for all responses so long!
Andreas Leppert

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

* Creating the same random numbers
  2011-01-29 23:41     ` Andreas Leppert
@ 2011-01-30  0:11       ` Manohar Vanga
  0 siblings, 0 replies; 8+ messages in thread
From: Manohar Vanga @ 2011-01-30  0:11 UTC (permalink / raw)
  To: kernelnewbies

>
> Because it would be easier to have something like srand(seed) and later
> on rand() instead of managing a list of generated numbers etc. Of
> course, providing a list of generated data could be a solution.
>

I don't know if this is helpful but you could always write a small function
to generate these deterministic "random" numbers within your modules
themselves.

http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators

As long as the initial conditions remain the same, the generated sets of
numbers should be the same. You could give the initial conditions as
parameters and generate multiple sets of data for multiple test runs in your
modules. I tried this with the simple xorshift algorithm and it generates
the same sets as long as the initial condition stays the same. Here's my
silly program below (sorry for the horrible variable naming):

#include <stdio.h>
#include <stdlib.h>

static unsigned int x = 123456789;
static unsigned int y = 362436069;
static unsigned int z = 521288629;
static unsigned int w = 88675123;

unsigned int xor128(void) {
unsigned int t;
t = x ^ (x << 11);
 x = y; y = z; z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}

int main(int argc, char *argv[])
{
int i;
if (argc > 1) {
 unsigned int j = atoi(argv[1]);
if (j != 0) {
/* Stick in some variations into the variables */
 x = j;
y = j << 2;
z = j >> 2;
 w = j >> 3;
}
}
 /* Generate a set of 100 at a time */
for (i = 0; i < 100; i++) {
 printf("%u\n", xor128());
}
}

Here are the test runs ("nsr" stands for "not so random"!):
$ ./nsr 1000 | tail -3
3972878080
46840635
1211846028
$ ./nsr 1000 | tail -3
3972878080
46840635
1211846028

$ ./nsr 1001 | tail -3
3176948974
4245070904
2954982611
$ ./nsr 1001 | tail -3
3176948974
4245070904
2954982611

I hope this helps! If not, I had fun trying it out anyway! Good luck!

-- 
/manohar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110130/7022f11c/attachment-0001.html 

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

* Creating the same random numbers
  2011-01-29 23:49     ` Andreas Leppert
@ 2011-01-30  0:34       ` Javier Martinez Canillas
  0 siblings, 0 replies; 8+ messages in thread
From: Javier Martinez Canillas @ 2011-01-30  0:34 UTC (permalink / raw)
  To: kernelnewbies

>> But in short answer yes, if you use the same seed the same answer
>> would be generated, assuming no further entropy is used later I.e
>> dependent on method employed to expand. Though then the 'randomness'
>> is in the seed not the output. Its this capability which allows simple
>> access control token devices to operate.
>>
> I'm not quite familiar with the number generators in the kernel.

Most random number generation functions are not truly random since
they are deterministic. To simulate a random behaviour those functions
are parametrized with a seed.

The kernel has a different approach and its random number generator
function is truly random. The kernel uses random events (hardware
interrupts) to feed an entropy pool. When random bytes are desired,
they are obtained by appliying the SHA hash of the contents of the
entropy pool.

Since hardware devices raises interrupts in a non-deterministic way,
the pool data is also non-deterministic and so are the values returned
by get_random_bytes().
>
> You mentioned the seed value. Is there a way to set a seed value before
> I invoke get_random_bytes? Could you direct me to a page / source file
> where I can get more information about it?
>

No, there isn't a seed for the get_random_bytes() for the reason i said before.

If you want a very high quality random number sequence and you want to
use that sequence with different functions, then I think your approach
is the best.

If you want to access from kernel space, use get_random_bytes() to
create the sequence, save the sequence and use it with all your
different functions.

On the other hand if you want to create the sequence from userpace,
read it from /dev/random that is only a interface that the kernel
exports to access the number random generator.

If you need a better understading of the kernel number generator look
at the code of drivers/char/random.c.

Best regards,

-- 
-----------------------------------------
Javier Mart?nez Canillas
(+34) 682 39 81 69
PhD Student in High Performance Computing
Computer Architecture and Operating System Department (CAOS)
Universitat Aut?noma de Barcelona
Barcelona, Spain

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

end of thread, other threads:[~2011-01-30  0:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-29 14:09 Creating the same random numbers Andreas Leppert
2011-01-29 19:34 ` Anuz Pratap Singh Tomar
2011-01-29 21:09   ` Enrico Granata
2011-01-29 23:38   ` Andreas Leppert
     [not found]   ` <1041152607-1296331182-cardhu_decombobulator_blackberry.rim.net-897203629-@b27.c7.bise7.blackberry>
2011-01-29 23:41     ` Andreas Leppert
2011-01-30  0:11       ` Manohar Vanga
2011-01-29 23:49     ` Andreas Leppert
2011-01-30  0:34       ` Javier Martinez Canillas

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.