Comp Sci How to Generate Random Numbers in C++ Using `<random>`?

Click For Summary
To generate random numbers in C++ without using the C library functions, the `<random>` library introduced in C++11 is recommended. This library allows for greater flexibility in selecting random number generators (RNGs) and distributions compared to the traditional `rand()` function. Users can set initial seeds and utilize various classes within `<random>` to create random integers or other distributions. While the library may have a learning curve, resources and documentation are available to aid in understanding its usage. For specific issues, users are encouraged to ask targeted questions after reviewing the API and available guides.
anonim
Messages
39
Reaction score
2
Homework Statement
-
Relevant Equations
-
I want to generate random numbers in C++. I do not want to use C library function (`<cstdlib> <ctime> (time.h)` ) and class. So I cannot use `rand()` function in C. I want to generate random integer numbers and I guess I can use `<random>` library in C++11. How can I use this generate random numbers?
 
Physics news on Phys.org
There is no reason that you have to use ctime to use the C-library rand(). Some examples use ctime to get a changing initial seed for an initial call to rand()'s srand() initialization function. You do not need to use that. You can set an initial seed using an unsigned integer of your choice in srand().

I have always assumed that the C++ version actually used the C rand().
 
anonim said:
How can I use this generate random numbers?

The documentation has examples. If you've already looked at them and need more help, please say so, what you've tried, what isn't working and where exactly you want help.

FactChecker said:
I have always assumed that the C++ version actually used the C rand().

It does not. One selects the RNG, and has many choices.
 
Vanadium 50 said:
It does not. One selects the RNG, and has many choices.
You might be right, but that is not very convincing. One often calls a pseudorandom number generator with a range of [0,1] and then converts the output to other ranges and distributions.
 
anonim said:
I want to generate random integer numbers and I guess I can use `<random>` library in C++11. How can I use this generate random numbers?

The classes in <random> are not super easy to use unless you know what you are doing, both regarding C++ and regarding the statistics your are trying to achieve, but once you got that they are quite flexible in allowing you to construct generators with all sorts of distributions. Since this is a homework question I would recommend you peek at the <random> API description with one eye and search for a good guide on the net with the other, and then return here with specific questions if you get stuck at some details.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
5
Views
5K
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 21 ·
Replies
21
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
7K