Generate random numbers using C++11 random library

smac89

As the title suggests, I am trying to figure out a way of generating random numbers using the new C++11 <random> library. I have tried it with this code:

std::default_random_engine generator;
std::uniform_real_distribution<double> uniform_distance(1, 10.001);

The problem with the code I have is that every time I compile and run it, it always generates the same numbers. So my question is what other functions in the random library can accomplish this while being truly random?

For my particular use case, I was trying to get a value within the range [1, 10]

Bill Lynch

Stephan T. Lavavej (stl) from Microsoft did a talk at Going Native about how to use the new C++11 random functions and why not to use rand(). In it, he included a slide that basically solves your question. I've copied the code from that slide below.

You can see his full talk here: http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

#include <random>
#include <iostream>

int main() {
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<double> dist(1.0, 10.0);

    for (int i=0; i<16; ++i)
        std::cout << dist(mt) << "\n";
}

We use random_device once to seed the random number generator named mt. random_device() is slower than mt19937, but it does not need to be seeded because it requests random data from your operating system (which will source from various locations, like RdRand for example).


Looking at this question / answer, it appears that uniform_real_distribution returns a number in the range [a, b), where you want [a, b]. To do that, our uniform_real_distibution should actually look like:

std::uniform_real_distribution<double> dist(1, std::nextafter(10, DBL_MAX));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generate random numbers in C++-11 in different parts of a code using the same seed

From Dev

Generate random numbers in C++-11 in different parts of a code using the same seed

From Dev

How to generate array of random numbers using functions in C

From Dev

Generate unique random numbers using SQL

From Dev

Generate random numbers on the fly using Intel MKL

From Dev

Using prime numbers to generate a random password

From Dev

Generate N random numbers using randomR

From Dev

Generate a random math Equation using Random numbers and operators in Javascript

From Dev

How to generate random numbers in C two colons?

From Dev

Generate random numbers without repetition in C

From Dev

generate a random string in C++11?

From Dev

Generate random numbers correctly

From Dev

Generate random numbers Jquery

From Dev

Generate random numbers

From Dev

Generate random numbers with Array

From Dev

Generate random numbers javascript

From Dev

Generate random numbers with exceptions

From Dev

Generate a list of random numbers

From Dev

Avoid same random numbers with same generator <random> c++ 11

From Dev

Avoid same random numbers with same generator <random> c++ 11

From Dev

Random numbers, C++11 vs Boost

From Dev

Creating an object of Random class or using Math.random() in order to generate random numbers

From Dev

Is random number generation in multiple threads using C++11 random library slow just like using rand() in multiple threads?

From Dev

Is random number generation in multiple threads using C++11 random library slow just like using rand() in multiple threads?

From Dev

How to generate random seed for random numbers

From Dev

Generate random textboxes to show random numbers

From Dev

How to generate 2 different random arrays of numbers using a C'tor in c++?

From Dev

Generate random string with FsCheck using C#

From Dev

Generate random numbers to be used within a table using ASP.NET C#

Related Related

  1. 1

    Generate random numbers in C++-11 in different parts of a code using the same seed

  2. 2

    Generate random numbers in C++-11 in different parts of a code using the same seed

  3. 3

    How to generate array of random numbers using functions in C

  4. 4

    Generate unique random numbers using SQL

  5. 5

    Generate random numbers on the fly using Intel MKL

  6. 6

    Using prime numbers to generate a random password

  7. 7

    Generate N random numbers using randomR

  8. 8

    Generate a random math Equation using Random numbers and operators in Javascript

  9. 9

    How to generate random numbers in C two colons?

  10. 10

    Generate random numbers without repetition in C

  11. 11

    generate a random string in C++11?

  12. 12

    Generate random numbers correctly

  13. 13

    Generate random numbers Jquery

  14. 14

    Generate random numbers

  15. 15

    Generate random numbers with Array

  16. 16

    Generate random numbers javascript

  17. 17

    Generate random numbers with exceptions

  18. 18

    Generate a list of random numbers

  19. 19

    Avoid same random numbers with same generator <random> c++ 11

  20. 20

    Avoid same random numbers with same generator <random> c++ 11

  21. 21

    Random numbers, C++11 vs Boost

  22. 22

    Creating an object of Random class or using Math.random() in order to generate random numbers

  23. 23

    Is random number generation in multiple threads using C++11 random library slow just like using rand() in multiple threads?

  24. 24

    Is random number generation in multiple threads using C++11 random library slow just like using rand() in multiple threads?

  25. 25

    How to generate random seed for random numbers

  26. 26

    Generate random textboxes to show random numbers

  27. 27

    How to generate 2 different random arrays of numbers using a C'tor in c++?

  28. 28

    Generate random string with FsCheck using C#

  29. 29

    Generate random numbers to be used within a table using ASP.NET C#

HotTag

Archive