Saturday, October 12, 2019

Digest for comp.lang.c++@googlegroups.com - 4 updates in 1 topic

Bonita Montero <Bonita.Montero@gmail.com>: Oct 12 09:40AM +0200

Is there any guarantee the standard makes that random_device shoudn't
start at the same internal state for all new threads, i.e. the output
of the following code is likey to be different on both lines?
 
#include <iostream>
#include <mutex>
#include <random>
#include <thread>
 
using namespace std;
 
int main()
{
auto thr = []()
{
static mutex mtx;
random_device rd;
mtx.lock();
cout << rd() << endl;
mtx.unlock();
};
thread t1( thr );
thread t2( thr );
t1.join();
t2.join();
}
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Oct 12 06:28PM +0200

On 12.10.2019 09:40, Bonita Montero wrote:
>     t1.join();
>     t2.join();
> }
 
As far as I know the standard allows `random_device` to be an ordinary
pseudo-random number generator with fixed seed, although that's clearly
not the intent.
 
To work around the MinGW g++ standard library's pseudo random
implementation, define _GLIBCXX_USE_RANDOM_TR1 before including any
standard library header, i.e. in the build.
 
For an easy-to-break in-source way to do it see <url:
https://github.com/alf-p-steinbach/cppx-core/blob/master/source/cppx-core/stdlib-wrappers/random-numbers-util.hpp>,
which also defines some simple wrappers. I should have fixed that, it
should be a conditional static_assert instead. You get the idea I hope.
 
 
- Alf
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Oct 12 12:58PM -0700

On 10/12/2019 12:40 AM, Bonita Montero wrote:
 
> Is there any guarantee the standard makes that random_device shoudn't
> start at the same internal state for all new threads, i.e. the output
> of the following code is likey to be different on both lines?
 
I don' think so, however, the term non-deterministic is used:
 
https://en.cppreference.com/w/cpp/numeric/random/random_device
 
A random device per thread should be okay. Although, one can create a
PRNG per thread and use a single random device to gain their individual
per-thread seeds.
 
 
 
Manfred <noname@add.invalid>: Oct 12 11:14PM +0200

On 10/12/2019 9:58 PM, Chris M. Thomasson wrote:
 
> A random device per thread should be okay. Although, one can create a
> PRNG per thread and use a single random device to gain their individual
> per-thread seeds.
 
The page also says that "In this case [PRNG] each std::random_device
object may generate the same number sequence."
 
Bottom line is that the properties random_device are implementation
dependent, so the authoritative source is the implementation, rather
than the standard.
 
In case of GCC this is what they say (which is not much):
https://gcc.gnu.org/onlinedocs/gcc-9.2.0/libstdc++/api/a06347.html
 
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: