Skip to contentSkip to conversation

Creating the same random numbers

DISCUSSION Andreas Leppert 5 participants 8 messages Jan 29, 2011 – Jan 30, 2011 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 14 lines · Andreas Leppert

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?

Quoted 8 lines

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

#2 lore raw

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

Quoted 24 lines · Anuz Pratap Singh Tomar

On Sat, Jan 29, 2011 at 2:09 PM, Andreas Leppert <[email protected]> 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

Quoted 4 lines

_______________________________________________
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

#3 lore raw
In reply to Anuz Pratap Singh Tomar

Hi,

Quoted 8 lines · Anuz Pratap Singh Tomar
Quoted 7 lines

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!

#5 lore raw
In reply to a message not in the archive

Hi Danny,

Quoted 2 lines · creslin at gmail.com

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.

#7 lore raw
In reply to a message not in the archive
Quoted 6 lines · creslin at gmail.com

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

#6 lore raw
In reply to Andreas Leppert
Quoted 5 lines

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!

Signature · 4 lines
/manohar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110130/7022f11c/attachment-0001.html 
#8 lore raw
In reply to Andreas Leppert
Quoted 7 lines
Quoted 6 lines

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

Quoted 5 lines

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,

Signature · 7 lines
-----------------------------------------
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