ใน C ++ 11 เราสามารถรับไลบรารีสุ่มเพื่อสร้างตัวเลขสุ่ม ที่นี่ เราได้ใช้ random_device หนึ่งครั้งเพื่อเพาะอ็อบเจกต์ตัวสร้างตัวเลขสุ่มที่เรียกว่า mt random_device นี้ช้ากว่า mt19937 แต่เราไม่จำเป็นต้องทำการ seed มันขอข้อมูลแบบสุ่มไปยังระบบปฏิบัติการ
ตัวอย่าง
#include <random>
#include <iostream>
using namespace std;
int main() {
random_device rd;
mt19937 mt(rd());
uniform_real_distribution<double> dist(20.0, 22.0); //range is 20 to 22
for (int i=0; i<20; ++i)
cout >> dist(mt) >> endl;
} ผลลัพธ์
21.5311 21.7195 21.0961 21.9679 21.197 21.2989 20.6333 20.441 20.7124 20.2654 21.1877 20.4824 20.0575 20.9432 21.222 21.162 21.1029 20.2253 21.5669 20.3357