Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Portable random number generator

Reply
Thread Tools

Portable random number generator

 
 
Gus Gassmann
Guest
Posts: n/a
 
      11-10-2010
I am collaborating on a rather large project with complicated XML
files (some objects nest ten levels deep) and corresponding data
handling challenges. I want to do proper testing of the (C++) code and
decided that the only way to go is random tests. So now I am looking
for a random number generator with the following properties:

1. Portability.
2. Random starting points.
3. Replicability on demand.

I presume this means that I would seed the RNG based on the clock, but
keep a copy of the seed that I could optionally use at the start in
case I found a problem on a previous run.

Statistical properties are of lesser importance.

I presume I am not the first person to attempt this and am hoping to
find some guidance here. Both C and C++ would be OK for the RNG, hence
the cross-post.

Thanks for any hints.

gus
--
comp.lang.c.moderated - moderation address: http://www.velocityreviews.com/forums/(E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      11-12-2010
Gus Gassmann <(E-Mail Removed)> writes:
>Thanks for any hints.


A »a portable implementation of rand and srand« is published
in ISO/IEC 9899:1999 (E), 7.20.2.2, #5.

 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      11-12-2010
On Nov 10, 6:12*am, Gus Gassmann <(E-Mail Removed)>
wrote:
> So now I am looking
> for a random number generator with the following properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.
>



Just use the K and R function

static unsigned long random_seed = 123456;

void simplesrand(unsigned long seed)
{
random_seed = seed;
}

int simplerand()
{
random_seed = random_seed * 1103515245 +12345;
return (unsigned int)(random_seed / 65536) % 32768;
}
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-12-2010
On Nov 10, 4:12 am, Gus Gassmann <(E-Mail Removed)>
wrote:
> I am collaborating on a rather large project with complicated XML
> files (some objects nest ten levels deep) and corresponding data
> handling challenges. I want to do proper testing of the (C++) code and
> decided that the only way to go is random tests. So now I am looking
> for a random number generator with the following properties:


> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.


> I presume this means that I would seed the RNG based on the clock, but
> keep a copy of the seed that I could optionally use at the start in
> case I found a problem on a previous run.


> Statistical properties are of lesser importance.


> I presume I am not the first person to attempt this and am hoping to
> find some guidance here. Both C and C++ would be OK for the RNG, hence
> the cross-post.


There are a number of implementations available on the network;
boost has some, for example. And it's not difficult to
implement the minimum generator yourself.

With regards to the seed: time() is the classical solution, but
depending on the context in which your program runs, it may not
suffice. On Unix platforms, I'll read a couple of bytes from
/dev/random. Otherwise, munging in things like the host IP
address and the process id may be necessary to ensure
uniqueness. (And once you've got a seed, log it so you can use
it in case of problems, like you said.)

--
James Kanze
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
Mark Wooding
Guest
Posts: n/a
 
      11-12-2010
Gus Gassmann <(E-Mail Removed)> writes:

> So now I am looking for a random number generator with the following
> properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.
>
> I presume this means that I would seed the RNG based on the clock, but
> keep a copy of the seed that I could optionally use at the start in
> case I found a problem on a previous run.
>
> Statistical properties are of lesser importance.


Replicability suggests a determinisic process: random starting points
are then your own problem of choosing a seed in some acceptable way.

There are a number of very simple and fast cryptographic-quality
generators out there (though you don't mention performance as being a
consideration at all).

Rivest's RC4 is extremely simple, and though it has some minor biases
seems adequate for non-cryptographic use if you can tolerate its
octet-at-a-time output.

Bernstein's Salsa20/8 is simple, fast, and seems very secure; it's also
seekable, which may or may not be of interest.

For non-cryptographic applications, I usually use Knuth's lagged
Fibonacci generator, which wants a lot of seed material; for this, I use
a linear congruential generator of my own devising.

I don't have an implementation of Salsa20/8, but you can surely find the
code online; the others are implemented in my Catacomb library, in
(pedantically) portable C:

http://git.distorted.org.uk/~mdw/catacomb/tree

available under the LGPL (see {rc4,fibrand,lc}.[ch]). Since this is for
testing purposes, I imagine the code won't in fact be distributed at all
and LGPL will therefore be acceptable. If I'm wrong about this, send me
mail: I'm willing to be generous with small portions of the library on a
case-by-case basis.

-- [mdw]
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      11-12-2010
On Nov 10, 6:12*am, Gus Gassmann <(E-Mail Removed)>
wrote:
> I am collaborating on a rather large project with complicated XML
> files (some objects nest ten levels deep) and corresponding data
> handling challenges. I want to do proper testing of the (C++) code and
> decided that the only way to go is random tests. So now I am looking
> for a random number generator with the following properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.
>
> I presume this means that I would seed the RNG based on the clock, but
> keep a copy of the seed that I could optionally use at the start in
> case I found a problem on a previous run.
>
> Statistical properties are of lesser importance.
>
> I presume I am not the first person to attempt this and am hoping to
> find some guidance here. Both C and C++ would be OK for the RNG, hence
> the cross-post.
>
> Thanks for any hints.


C++0x will contain a clone of boost::random. You can do pretty much
everything with that lib. All generators have their state seedable and
serializable.

Note, that random testing is very powerful tool, but common solution
there is not seedable RNG. Common solution is to automatically
extract exact minimal amount of "steps to reproduce" from noise of
(for example) 300 random operations that resulted with a problem. Then
report/store these as a problem case.
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-12-2010
On 11/9/2010 11:12 PM, Gus Gassmann wrote:
> I am collaborating on a rather large project with complicated XML
> files (some objects nest ten levels deep) and corresponding data
> handling challenges. I want to do proper testing of the (C++) code and
> decided that the only way to go is random tests. So now I am looking
> for a random number generator with the following properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.
>
> I presume this means that I would seed the RNG based on the clock, but
> keep a copy of the seed that I could optionally use at the start in
> case I found a problem on a previous run.
>
> Statistical properties are of lesser importance.
>
> I presume I am not the first person to attempt this and am hoping to
> find some guidance here. Both C and C++ would be OK for the RNG, hence
> the cross-post.
>
> Thanks for any hints.


The C Standard shows a portable example of a way its rand()
function could be implemented; you could take that code, change
the name, file off the serial numbers, and use it.

Depending on what you mean by "replicability," you may or may
not be able to use rand(). On any given system, rand() will be
repeatable: seed it with the same srand() argument, and you'll get
the same sequence of rand() results. But if you need to get the
same sequence on System B that you got earlier on System A, this
mightn't work: A's and B's rand() implementations may differ.
(Some people mistakenly believe that the Standard's example is *the* way
to implement rand(), but it's only an example.) So if you need
agreement across systems, you'll need to incorporate your own code
and not rely on the local rand().

--
Eric Sosman
(E-Mail Removed)lid
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
Dag-Erling Smørgrav
Guest
Posts: n/a
 
      11-12-2010
Gus Gassmann <(E-Mail Removed)> writes:
> I am collaborating on a rather large project with complicated XML
> files (some objects nest ten levels deep) and corresponding data
> handling challenges. I want to do proper testing of the (C++) code and
> decided that the only way to go is random tests. So now I am looking
> for a random number generator with the following properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.


There are plenty of PRNGs (with the P meaning "pseudo", not "portable")
that meet those criteria. Wikipedia is a good starting point,
especially the Mersenne twister article, which includes both pseudocode
and links to existing implementations. IIUC, the Mersenne twister is
currently the best known non-cryptographic deterministic PRNG.

DES
--
Dag-Erling Smørgrav - (E-Mail Removed)
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      11-12-2010
Gus Gassmann wrote:
> I am collaborating on a rather large project with complicated XML
> files (some objects nest ten levels deep) and corresponding data
> handling challenges. I want to do proper testing of the (C++) code and
> decided that the only way to go is random tests. So now I am looking
> for a random number generator with the following properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.


I'm not sure if all of these are supported, but certainly you should
look at:

http://www.boost.org/doc/libs/1_44_0...st_random.html

Jeff
--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
DDD
Guest
Posts: n/a
 
      11-12-2010
Gus Gassmann ha escrito:
> I am collaborating on a rather large project with complicated XML
> files (some objects nest ten levels deep) and corresponding data
> handling challenges. I want to do proper testing of the (C++) code and
> decided that the only way to go is random tests. So now I am looking
> for a random number generator with the following properties:
>
> 1. Portability.
> 2. Random starting points.
> 3. Replicability on demand.
>
> I presume this means that I would seed the RNG based on the clock, but
> keep a copy of the seed that I could optionally use at the start in
> case I found a problem on a previous run.
>
> Statistical properties are of lesser importance.
>
> I presume I am not the first person to attempt this and am hoping to
> find some guidance here. Both C and C++ would be OK for the RNG, hence
> the cross-post.
>
> Thanks for any hints.


If there is no exist functions for using, why not writing yourself.
For example, by getting system time in seconds and then...

>
> gus
> --
> comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. Sorry.

--
comp.lang.c.moderated - moderation address: (E-Mail Removed) -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Portable random number generator Gus Gassmann C++ 43 11-23-2010 09:51 PM
Portable random number generator Gus Gassmann C Programming 2 11-17-2010 07:36 PM
Portable random number generator Gus Gassmann C++ 2 11-17-2010 07:36 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM



Advertisments