Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GetRandomUniform

 

STLQt CreatorLubuntu

 

GetRandomUniform is a random code snippet to draw a value from 0.0 to (and not including) 1.0. All values have an equal likelyhood to be drawn.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppGetRandomUniform/CppGetRandomUniform.pro

 

include("../../ConsoleApplication.pri")
SOURCES += main.cpp

 

 

 

 

 

./CppGetRandomUniform/main.cpp

 

#include <cassert>
#include <random>
#include <iostream>

///GetRandomUniform draws a random number from 0.0 to and excluding 1.0.
///From http://www.richelbilderbeek.nl/CppGetRandomUniform.htm
double GetRandomUniform()
{
  //rd is used only to initialize mt with a truly random seed
  static std::random_device rd;
  //mt generates random numbers
  static std::mt19937 mt(rd());
  //d puts these random numbers in the correct distribution
  static std::uniform_real_distribution<double> d(0.0,1.0);
  //The random value x gets drawn here
  const double x{d(mt)};
  return x;
}

int main()
{
  for (int i=0; i!=10; ++i)
  {
    const double x{GetRandomUniform()};
    assert(x >= 0.0 && x < 1.0);
    std::cout << x << '\n';
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml