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

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++ Numbers Random
Click For Summary

Discussion Overview

The discussion centers around generating random numbers in C++ using the `` library introduced in C++11. Participants explore alternatives to the C library's `rand()` function and seek guidance on how to effectively utilize the features of the `` library for generating random integers.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses a desire to generate random integer numbers using the `` library without relying on the C library's `rand()` function.
  • Another participant notes that while some examples use `ctime` for seeding `rand()`, it is not necessary to do so, as an unsigned integer can be used for seeding.
  • There is a suggestion that the C++ random number generation might still be based on the C `rand()`, though this is challenged by another participant who asserts that C++ allows for the selection of different random number generators (RNGs).
  • A participant mentions that calling a pseudorandom number generator typically involves generating numbers in the range of [0,1] before converting them to other ranges and distributions.
  • One participant warns that the classes in `` can be complex and recommends reviewing the API documentation and seeking additional guides for better understanding before returning with specific questions.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether the C++ random number generation is based on the C `rand()` function. There are multiple viewpoints regarding the use of the `` library and its complexity.

Contextual Notes

Some participants express uncertainty about the ease of use of the `` library and the statistical knowledge required to effectively implement it. There are also unresolved questions about the specific implementation details and examples needed for generating random numbers.

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
2K
Replies
5
Views
5K
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 21 ·
Replies
21
Views
8K
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K