All of lore.kernel.org
 help / color / mirror / Atom feed
From: manohar.vanga@gmail.com (Manohar Vanga)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Creating the same random numbers
Date: Sun, 30 Jan 2011 01:11:33 +0100	[thread overview]
Message-ID: <AANLkTimsdL3x_vPok8Ar7LnSK23Xg3=jQXDirnqcB82o@mail.gmail.com> (raw)
In-Reply-To: <20110129234119.GC3225@yoda>

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

  reply	other threads:[~2011-01-30  0:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2011-01-29 23:49     ` Andreas Leppert
2011-01-30  0:34       ` Javier Martinez Canillas

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='AANLkTimsdL3x_vPok8Ar7LnSK23Xg3=jQXDirnqcB82o@mail.gmail.com' \
    --to=manohar.vanga@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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 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.