How do I generate thread-safe uniform random numbers?

PThomasCS

My program needs to generate many random integers in some range (int min, int max). Each call will have a different range. What is a good (preferably thread-safe) way to do this? The following is not thread-safe (and uses rand(), which people seem to discourage):

int intRand(const int & min, const int & max)
{
    return (rand() % (max+1-min)) + min;
}

This is much slower, but uses <random>:

int intRand(const int & min, const int & max) {
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(min,max);
    return distribution(generator);
}

Something like this is what I'm going for (the changeParameters function doesn't exist though):

int intRand(const int & min, const int & max) {
    static std::default_random_engine generator;
    static std::uniform_int_distribution<int> distribution(0, 10);
    distribution.changeParameters(min, max);
    return distribution(generator);
}

Another option would be to make a wide range on the uniform_int_distribution and then use mod like in the first example. However, I'm doing statistical work, so I want the numbers to come from as unbiased of a distribution as possible (e.g., if the range of the distribution used is not a multiple of (max-min), the distribution will be slightly biased). This is an option, but again, I would like to avoid it.

SOLUTION This solution comes from the answers by @konrad-rudolph @mark-ransom and @mathk . The seeding of the random number generator is done to suit my particular needs. A more common approach would be to use time(NULL). If you make many threads in the same second, they would then get the same seed though. Even with clock() this is an issue, so we include the thread id. A drawback - this leaks memory --- one generator per thread.

#if defined (_MSC_VER)  // Visual studio
    #define thread_local __declspec( thread )
#elif defined (__GCC__) // GCC
    #define thread_local __thread
#endif

#include <random>
#include <time.h>
#include <thread>

using namespace std;

/* Thread-safe function that returns a random number between min and max (inclusive).
This function takes ~142% the time that calling rand() would take. For this extra
cost you get a better uniform distribution and thread-safety. */
int intRand(const int & min, const int & max) {
    static thread_local mt19937* generator = nullptr;
    if (!generator) generator = new mt19937(clock() + this_thread::get_id().hash());
    uniform_int_distribution<int> distribution(min, max);
    return distribution(*generator);
}
Konrad Rudolph

Have you tried this?

int intRand(const int & min, const int & max) {
    static thread_local std::mt19937 generator;
    std::uniform_int_distribution<int> distribution(min,max);
    return distribution(generator);
}

Distributions are extremely cheap (they will be completely inlined by the optimiser so that the only remaining overhead is the actual random number rescaling). Don’t be afraid to regenerate them as often as you need – in fact, resetting them would conceptually be no cheaper (which is why that operation doesn’t exist).

The actual random number generator, on the other hand, is a heavy-weight object carrying a lot of state and requiring quite some time to be constructed, so that should only be initialised once per thread (or even across threads, but then you’d need to synchronise access which is more costly in the long run).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I generate thread-safe uniform random numbers?

From Dev

How to generate random numbers in a thread safe way with OpenMP

From Dev

How do I generate a sequence of integer numbers in a uniform distribution?

From Dev

How do I generate a sequence of integer numbers in a uniform distribution?

From Dev

How do I generate random numbers to fill in empty rows?

From Dev

How do I generate a number of random numbers that are all odd

From Dev

How do I generate random numbers from [0,1]?

From Dev

How do I generate big random numbers in Dart?

From Dev

How do I generate random numbers with a mean in C++?

From Dev

How do I generate 8 different random numbers in python?

From Dev

How do I generate a random range of numbers within another range?

From Dev

How do I generate different random numbers in a loop in C?

From Dev

How do I generate random numbers to fill in empty rows?

From Dev

How do I generate a random list in Python with duplicates numbers

From Dev

How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

From Dev

How do computers generate random numbers

From Dev

C++ thread-safe uniform distribution random number generation

From Dev

How many random numbers can std::uniform_real_distribution generate before losing randomness?

From Dev

Getting random numbers in a thread-safe way

From Dev

How do I get a random number/letter generator to generate 3 Letters then 6 Numbers? in WPF

From Dev

How do I generate non-repeating random numbers in a while loop? (Python 3)

From Dev

Generate random numbers with uniform distribution (getting same number in loop)

From Dev

Generate random numbers with uniform distribution (getting same number in loop)

From Java

How do I generate a random int number?

From Dev

How do I generate a random uint with a maximum?

From Dev

How do I populate an array with random numbers?

From Dev

How do I generate a sequence of numbers like this?

From Java

How to generate random numbers which do not include zeros?

From Dev

How can I generate two random numbers(not repeated)?

Related Related

  1. 1

    How do I generate thread-safe uniform random numbers?

  2. 2

    How to generate random numbers in a thread safe way with OpenMP

  3. 3

    How do I generate a sequence of integer numbers in a uniform distribution?

  4. 4

    How do I generate a sequence of integer numbers in a uniform distribution?

  5. 5

    How do I generate random numbers to fill in empty rows?

  6. 6

    How do I generate a number of random numbers that are all odd

  7. 7

    How do I generate random numbers from [0,1]?

  8. 8

    How do I generate big random numbers in Dart?

  9. 9

    How do I generate random numbers with a mean in C++?

  10. 10

    How do I generate 8 different random numbers in python?

  11. 11

    How do I generate a random range of numbers within another range?

  12. 12

    How do I generate different random numbers in a loop in C?

  13. 13

    How do I generate random numbers to fill in empty rows?

  14. 14

    How do I generate a random list in Python with duplicates numbers

  15. 15

    How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

  16. 16

    How do computers generate random numbers

  17. 17

    C++ thread-safe uniform distribution random number generation

  18. 18

    How many random numbers can std::uniform_real_distribution generate before losing randomness?

  19. 19

    Getting random numbers in a thread-safe way

  20. 20

    How do I get a random number/letter generator to generate 3 Letters then 6 Numbers? in WPF

  21. 21

    How do I generate non-repeating random numbers in a while loop? (Python 3)

  22. 22

    Generate random numbers with uniform distribution (getting same number in loop)

  23. 23

    Generate random numbers with uniform distribution (getting same number in loop)

  24. 24

    How do I generate a random int number?

  25. 25

    How do I generate a random uint with a maximum?

  26. 26

    How do I populate an array with random numbers?

  27. 27

    How do I generate a sequence of numbers like this?

  28. 28

    How to generate random numbers which do not include zeros?

  29. 29

    How can I generate two random numbers(not repeated)?

HotTag

Archive