एकीकृत और नमूने पहले से ही मैं करने के लिए संक्षेप में प्रस्तुत करेंगे प्रदान किए गए कुछ सरल बनाने:
// Good random seed, good engine
auto rnd1 = std::mt19937(std::random_device{}());
// Good random seed, default engine
auto rnd2 = std::default_random_engine(std::random_device{}());
// like rnd1, but force distribution to int32_t range
auto rnd3 = std::bind(std::uniform_int_distribution<int32_t>{}, std::mt19937(std::random_device{}()));
// like rnd3, but force distribution across negative numbers as well
auto rnd4 = std::bind(std::uniform_int_distribution<int32_t>{std::numeric_limits<int32_t>::min(),std::numeric_limits<int32_t>::max()}, std::mt19937(std::random_device{}()));
तो मैं कुछ परीक्षण चलाया, तो चूक की तरह लग रही देखने के लिए:
#include <random>
#include <functional>
#include <limits>
#include <iostream>
template<class Func>
void print_min_mean_max(Func f) {
typedef decltype(f()) ret_t;
ret_t min = std::numeric_limits<ret_t>::max(), max = std::numeric_limits<ret_t>::min();
uint64_t total = 0, count = 10000000;
for (uint64_t i = 0; i < count; ++i) {
auto res = f();
min = std::min(min,res);
max = std::max(max,res);
total += res;
}
std::cout << "min: " << min << " mean: " << (total/count) << " max: " << max << std::endl;
}
int main() {
auto rnd1 = std::mt19937(std::random_device{}());
auto rnd2 = std::default_random_engine(std::random_device{}());
auto rnd3 = std::bind(std::uniform_int_distribution<int32_t>{}, std::mt19937(std::random_device{}()));
auto rnd4 = std::bind(std::uniform_int_distribution<int32_t>{std::numeric_limits<int32_t>::min(),std::numeric_limits<int32_t>::max()}, std::mt19937(std::random_device{}()));
print_min_mean_max(rnd1);
print_min_mean_max(rnd2);
print_min_mean_max(rnd3);
print_min_mean_max(rnd4);
}
का उत्पादन आउटपुट:
min: 234 mean: 2147328297 max: 4294966759
min: 349 mean: 1073305503 max: 2147483423
min: 601 mean: 1073779123 max: 2147483022
min: -2147481965 mean: 178496 max: 2147482978
तो जैसा कि हम देख सकते हैं, mt19937 और default_random_engine की एक अलग डिफ़ॉल्ट सीमा है, इसलिए uniform_int_distribution का उपयोग करने की सलाह दी जाती है।
इसके अलावा, डिफ़ॉल्ट वर्दी_इन_डिस्ट्रिब्यूशन [0, max_int] (गैर-नकारात्मक) है, यहां तक कि एक हस्ताक्षरित पूर्णांक प्रकार का उपयोग करते समय भी। यदि आप पूर्ण सीमा चाहते हैं तो सीमा को स्पष्ट रूप से प्रदान करना होगा।
अंत में, its important to remember this इन दिनों कभी-कभी।
विकिपीडिया? http://en.wikipedia.org/wiki/C%2B%2B0x#Extensible_random_number_facility – quasiverse
आपके पास कोड के साथ क्या गलत है? AFAIK, नए यादृच्छिक संख्या जनरेटर को अधिक "गंभीर" अनुप्रयोगों के लिए जोड़ा गया था जहां यादृच्छिक संख्या पीढ़ी के पहलू वास्तव में मायने रखते हैं। – GManNickG
@GMan: निष्पक्ष होने के लिए, नए मानक में यादृच्छिक संख्या इंजनों में से कई को सरल और तेज़ के रूप में वर्णित किया जा सकता है और मैं उन्हें विशेष रूप से "गंभीर" के रूप में नहीं देखूंगा। –