# Creating the same random numbers

Manohar Vanga manohar.vanga at gmail.com
Sat Jan 29 19:11:33 EST 2011

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