C++随机数
1 //随机数(头文件random) 2 // auto random = bind(uniform_int_distribution<>{1, 6}, default_random_engine());//需要头文件functional 3 // cout << random() << endl; 4 struct Random 5 { 6 Random(int low, int high) :dist{ low,high } {} 7 int operator()() 8 { 9 return dist(engine); 10 } 11 uniform_int_distribution<>dist;//分布 12 default_random_engine engine;//引擎 13 }; 14 Random r(0, 4); 15 vector<int>arr(5); 16 for (int i = 0; i < 100; i++) 17 { 18 ++arr[r()]; 19 } 20 for (int i = 0; i < arr.size(); i++) 21 { 22 cout << i << "\t"; 23 for (int j = 0; j < arr[i]; j++) 24 { 25 cout << "*"; 26 } 27 cout << endl; 28 }
运行效果: