Skip to content

Instantly share code, notes, and snippets.

@skairunner
Created February 1, 2014 11:03
Show Gist options
  • Save skairunner/8750791 to your computer and use it in GitHub Desktop.
Save skairunner/8750791 to your computer and use it in GitHub Desktop.
#include <random>
#include <time.h>
#include <iostream>
// Source: http://www.cplusplus.com/reference/random/
// Source: http://en.cppreference.com/w/cpp/numeric/random
int main()
{
// Starting up the rng
std::mt19937 rng;
// Seeding the rng
rng.seed(time(0));
// Getting a random INTEGER in a range
std::uniform_int_distribution<> dist(0, 10); // closed interval, meaning a <= x <= b
std::cout << dist(rng) << std::endl;
// To get another random integer
std::cout << dist(rng) << std::endl;
// Get a different range of integers
std::uniform_int_distribution<> dist2(15, 25);
std::cout << dist2(rng) << std::endl;
// Same for floating point numbers
std::uniform_real_distribution<> dist3(0.0, 1.0); // semi-open interval,a <= x < b
std::cout << dist3(rng) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment