1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <random> 11 12 // template<class RealType, size_t bits, class URNG> 13 // RealType generate_canonical(URNG& g); 14 15 #include <random> 16 #include <cassert> 17 18 int main() 19 { 20 { 21 typedef std::minstd_rand0 E; 22 typedef float F; 23 E r; 24 F f = std::generate_canonical<F, 0>(r); 25 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 26 } 27 { 28 typedef std::minstd_rand0 E; 29 typedef float F; 30 E r; 31 F f = std::generate_canonical<F, 1>(r); 32 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 33 } 34 { 35 typedef std::minstd_rand0 E; 36 typedef float F; 37 E r; 38 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r); 39 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 40 } 41 { 42 typedef std::minstd_rand0 E; 43 typedef float F; 44 E r; 45 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r); 46 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 47 } 48 { 49 typedef std::minstd_rand0 E; 50 typedef float F; 51 E r; 52 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r); 53 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 54 } 55 56 { 57 typedef std::minstd_rand0 E; 58 typedef double F; 59 E r; 60 F f = std::generate_canonical<F, 0>(r); 61 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 62 } 63 { 64 typedef std::minstd_rand0 E; 65 typedef double F; 66 E r; 67 F f = std::generate_canonical<F, 1>(r); 68 assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1))); 69 } 70 { 71 typedef std::minstd_rand0 E; 72 typedef double F; 73 E r; 74 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r); 75 assert(f == 76 (16807 - E::min() + 77 (282475249 - E::min()) * (E::max() - E::min() + F(1))) / 78 ((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))); 79 } 80 { 81 typedef std::minstd_rand0 E; 82 typedef double F; 83 E r; 84 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r); 85 assert(f == 86 (16807 - E::min() + 87 (282475249 - E::min()) * (E::max() - E::min() + F(1))) / 88 ((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))); 89 } 90 { 91 typedef std::minstd_rand0 E; 92 typedef double F; 93 E r; 94 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r); 95 assert(f == 96 (16807 - E::min() + 97 (282475249 - E::min()) * (E::max() - E::min() + F(1))) / 98 ((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))); 99 } 100 } 101