VB icon

Random numbers in C++11

Submitted on: 3/22/2020 9:50:56 AM
By: Thomas1965 
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general)
Views: 702
     Simple demo that shows how to generate random numbers(int and double) in C++11. Written in VS 2017

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Random numbers in C++11
// Description:Simple demo that shows how to generate random numbers(int and double) in C++11.
Written in VS 2017
// By: Thomas1965

#include <iostream>
#include <random>
class Random
 int next_int(int min_val, int max_val);
 double next_double(double min_val, double max_val);
 std::default_random_engine engine;
int Random::next_int(int min_val, int max_val)
 std::uniform_int_distribution<int> di(min_val, max_val);
 return di(engine);
double Random::next_double(double min_val, double max_val)
 std::uniform_real_distribution<double> di(min_val, max_val);
 return di(engine);
constexpr int MinInt = 1;
constexpr int MaxInt = 100;
constexpr double MinDouble = -99.9;
constexpr double MaxDouble = 99.9;
int main()
 Random rnd;
 std::cout << "Random test\n";
 std::cout << "-----------\n";
 std::cout << "Generating 10 random ints in the range of " 
<< MinInt << '-' << MaxInt << '\n';
 for (int i = 0; i < 10; ++i)
std::cout << rnd.next_int(MinInt, MaxInt) << ' ';
 std::cout << "\n\n\n";
 std::cout << "Generating 10 random doubles in the range of "
<< MinDouble << '-' << MaxDouble << '\n';
 for (int i = 0; i < 10; ++i)
std::cout << rnd.next_double(MinDouble, MaxDouble) << ' ';
 std::cout << "\n\n\n";

Other 31 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.