parallelizing random number generator suggestions?

I am trying to randomize this random_number generator, however , as i am new to c++ I am struggling to do so. any suggestions as to what steps i should take ?

i have posted the function i am using below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
void mutate(double mutation_rate, genome &gen)
{
	static default_random_engine e(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count());
	static uniform_real_distribution<double> dist;
	double rnd;
	auto num_threads = thread::hardware_concurrency();

#pragma omp parallel for num_threads(num_threads)
		for (signed int bit = 0; bit < gen.bits.size(); ++bit)
			{
				rnd = dist(e);
				if (rnd < mutation_rate)
					gen.bits[bit] = !gen.bits[bit];
			}
}


I am trying to randomize this random_number generator

What does it mean to "randomize this random_number generator"? A random number generator is already random; how can you randomize it?

What does this function not do that you want it to do?
Last edited on
I think (from the original title and the OpenMP pragma) its a typo - read "parallelise this random-number generator" rather than "randomise this random-number generator".

If you are new to C++ ... writing code for parallel processing seems a bit of a daunting task!
This code uses OMP to generate random numbers in parallel.

#include "omp.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <random>
#include <iostream>

int main()
{

  std::random_device r;
  std::default_random_engine e1(r());

 int th_id;
 #pragma omp parallel private(th_id)
 {
   th_id = omp_get_thread_num();
   int randomNum =  e1();

   #pragma omp critical
   std::cout << "Thread " << th_id << " says: " << e1() << '\n';
  }
}



However, I believe that there is no guarantee of thread safety with the random number generators, so really we should have one engine per thread (i.e. the engines created within the parallel section, rather than one shared between them); see http://stackoverflow.com/questions/21237905/how-do-i-generate-thread-safe-uniform-random-numbers and other such.

Topic archived. No new replies allowed.