Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 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: -- 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
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-10-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.


I am afraid to ask... Why not use 'srand/rand' pair of functions? You
can always do

unsigned seed = (unsigned)time(0); // for keeping
srand(seed);
...

Of course, it's so damn obvious that I expect some kind of a trick...

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      11-10-2010
"Victor Bazarov" wrote:

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

>
> I am afraid to ask... Why not use 'srand/rand' pair of functions? You
> can always do
>
> unsigned seed = (unsigned)time(0); // for keeping
> srand(seed);
> ...
>
> Of course, it's so damn obvious that I expect some kind of a trick...


I assume by portable he means the same sequence would be provided from a
given seed regardless of the target computer.

rand() gets an F on that.


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-10-2010
Victor Bazarov <> wrote:
> I am afraid to ask... Why not use 'srand/rand' pair of functions?


It's indeed the standard solution, but only if the horrendous quality
of the typical std::rand() implementation isn't a problem.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-10-2010
On 11/10/2010 10:11 AM, osmium wrote:
> "Victor Bazarov" wrote:
>
>> 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.

>>
>> I am afraid to ask... Why not use 'srand/rand' pair of functions? You
>> can always do
>>
>> unsigned seed = (unsigned)time(0); // for keeping
>> srand(seed);
>> ...
>>
>> Of course, it's so damn obvious that I expect some kind of a trick...

>
> I assume by portable he means the same sequence would be provided from a
> given seed regardless of the target computer.


Ah... I didn't assume that. But it might be valid, if he wants to run
the failed tests on a different platform than the one on which they fail.

> rand() gets an F on that.


In that case he just needs to take the source of the 'rand' from any
standard library implementation that he can lay his hands on, and stick
it into his source. From what I gathered of the requirements the
evenness of the distribution or the length of the cycle aren't high on
his list.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      11-10-2010
"Victor Bazarov" wrote:

> On 11/10/2010 10:11 AM, osmium wrote:
>> "Victor Bazarov" wrote:
>>
>>> 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.
>>>
>>> I am afraid to ask... Why not use 'srand/rand' pair of functions? You
>>> can always do
>>>
>>> unsigned seed = (unsigned)time(0); // for keeping
>>> srand(seed);
>>> ...
>>>
>>> Of course, it's so damn obvious that I expect some kind of a trick...

>>
>> I assume by portable he means the same sequence would be provided from a
>> given seed regardless of the target computer.

>
> Ah... I didn't assume that. But it might be valid, if he wants to run the
> failed tests on a different platform than the one on which they fail.
>
>> rand() gets an F on that.

>
> In that case he just needs to take the source of the 'rand' from any
> standard library implementation that he can lay his hands on, and stick it
> into his source. From what I gathered of the requirements the evenness of
> the distribution or the length of the cycle aren't high on his list.


I don't think that works. Many of the generators that I have looked at have
built in assumptions; computations are assumed to be mod 16 or mod 32, for
example based on the (PRNG) programmer's presuming a certain platform.
These are implicit assumptions and don't show up in the code. For example,
I wouldn't be happy if I saw a simple variable of type int in the code,
since int has no particular meaning.

From the general tenor of the original request, I think he wants testing on
various platforms to exhibit consistent results.


 
Reply With Quote
 
Andy Venikov
Guest
Posts: n/a
 
      11-10-2010
On 11/10/2010 10:19 AM, Juha Nieminen wrote:
> Victor Bazarov<> wrote:
>> I am afraid to ask... Why not use 'srand/rand' pair of functions?

>
> It's indeed the standard solution, but only if the horrendous quality
> of the typical std::rand() implementation isn't a problem.


If performance isn't an issue, you can use ARCFOUR - it produces the
same sequence given the same seed on all architectures (since it's
byte-based), the randomness is fairly good and it's SO easy to implement.

Andy.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-10-2010
On Wed, 2010-11-10, Juha Nieminen wrote:
> Victor Bazarov <> wrote:
>> I am afraid to ask... Why not use 'srand/rand' pair of functions?

>
> It's indeed the standard solution, but only if the horrendous quality
> of the typical std::rand() implementation isn't a problem.


Which ones are you thinking of, specifically? And what's horrendous
about them?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-11-2010
On Nov 10, 10:21 pm, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
> On Wed, 2010-11-10, Juha Nieminen wrote:
> > Victor Bazarov <v.baza...@comcast.invalid> wrote:
> >> I am afraid to ask... Why not use 'srand/rand' pair of functions?


> > It's indeed the standard solution, but only if the
> > horrendous quality of the typical std::rand() implementation
> > isn't a problem.


> Which ones are you thinking of, specifically? And what's horrendous
> about them?


Historically, many rand() were horrible; I don't know if it's
still a problem, but the (non-normative) example for rand() in
C90 was horrible, and it corresponded to a widely used
implementation. (The C90 committee didn't invent the
implementation; they just copied the most widely used one.)

Today, the obvious solution is to use the random generators from
Boost: they have a number of them, with a common interface, so
you can choose the balance between "randomness" and speed which
corresponds to your needs.

For the seed, the choice of time() is OK for programs running
independently on a machine. More generally, however, something
more elaborate may be desirable. I'd just read however many
bytes I needed from /dev/random under Unix; otherwise, you can
munge in such values as the machine IP or (better) MAC address,
the process id, etc., to get something truely unique.

--
James Kanze
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-11-2010
Jorgen Grahn <grahn+> wrote:
> On Wed, 2010-11-10, Juha Nieminen wrote:
>> Victor Bazarov <> wrote:
>>> I am afraid to ask... Why not use 'srand/rand' pair of functions?

>>
>> It's indeed the standard solution, but only if the horrendous quality
>> of the typical std::rand() implementation isn't a problem.

>
> Which ones are you thinking of, specifically? And what's horrendous
> about them?


All the implementations of std::rand() I know of use a simple 32-bit
linear congruential generator (which result is, for whatever reason I
can't really understand, clamped to 15 bits). Linear congruential
generators are some of the poorest RNGs around, and often present aliasing
artifacts in certain situations (eg. regular patterns). Also, oddly enough
(taking into accoung how simple a LCG is, basically one integer
multiplication and one addition), LCGs are rather slow (of course this is
only relevant if you need extreme efficiency).

There are many RNG algorithms which produce significantly better-quality
distributions significantly faster than a typical LCG-using std::rand().
(The most known example is probably the Mersenne Twister, but there are
others too, such as ISAAC.)
 
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 Programming 38 11-23-2010 10:40 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