Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created August 27, 2014 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwin/bf89d957b97502236ed9 to your computer and use it in GitHub Desktop.
Save ashwin/bf89d957b97502236ed9 to your computer and use it in GitHub Desktop.
Random number generation in C++11
#include <iostream>
#include <limits>
#include <random>
int main()
{
std::default_random_engine eng((std::random_device())());
std::uniform_int_distribution<int8_t> idis(0, std::numeric_limits<int8_t>::max());
for (int i = 0; i < 10; ++i)
{
int8_t v = idis(eng);
std::cout << (int32_t) v << std::endl;
}
std::uniform_real_distribution<float> fdis(0, 2.0);
for (int i = 0; i < 10; ++i)
{
float v = fdis(eng);
std::cout << v << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment