【C++ 程序】 随机数

tech2025-07-06  2

背景

这是一个真实的使用场景(2020.9.4)。

新生集训营中每半天都要2位学生写一篇感想。其中有5位同学在之前一天已主动报名写好了读后感。于是应该编写一个程序来为剩下的同学排序,选出幸运的同学来完成这个任务。

随机数

随机数有几种,一种是使用时间函数定出的随机数,程序示例见 【C++ 程序】 井字棋游戏(人 VS Lv1电脑)中Line 59-60。 另外,可以用直接电脑自带的随机数函数来生成随机数:

示例程序

#include <iostream> #include <random> using namespace std; int main() { vector<unsigned> vec, vint; unsigned alr = 0, iter = 0; default_random_engine random; for (int i = 0; i != 1000; i++) { unsigned ran = random() % 31 + 1; vec.push_back(ran); } cout << "Let's choose the lucky dogs." << endl; for (unsigned iter = 0; iter != 1000; iter++) { if (vec[iter] != 5 && vec[iter] != 11 && vec[iter] != 12 && vec[iter] != 22 && vec[iter] != 26) { unsigned id = 0; for (unsigned i = 0; i != iter; i++) { if (vec[i] == vec[iter]) id = 1; } if (id == 0) { vint.push_back(vec[iter]); ++alr; } } if (alr == 20) break; } for (unsigned j = 0; j != 20; j++) { if (j % 4 == 0) cout << "\nSept." << j / 4 + 4 << ": "; if (j % 4 == 2) cout << "| "; if (vint[j] < 10)cout << "0"; cout << vint[j] << " "; } cout << endl; return 0; }

样例输出

分析

这个程序每次输出是一样的,因为这其实是个随机数表。 此处没有用uniform_int_distribution<unsigned> u(max, min),因为似乎直接用%也挺方便的。 此方法详见我的博客 【笔记】 C++中 随机数各方式总结。

另一案例

#include <iostream> #include <vector> #include <random> #include <ctime> #include <string> using namespace std; vector<unsigned> already{ 20,22,26,27 }; int not_already(int n) { unsigned cnt_n = 0; for (auto c : already) if (c != n) ++cnt_n; if (cnt_n != already.size()) return 0; // already chosen else return 1; // not chosen } int main() { vector<unsigned> random_vec; srand((unsigned)time(NULL)); int ran_temp = rand() % 10; // generate a random number default_random_engine random; for (int i = ran_temp; i != 1000 + ran_temp; i++) { unsigned ran = random() % 24 + 8; // generate another random number random_vec.push_back(ran); } unsigned cnt = 0; unsigned index = 0; for (int i = 0; i != 1000; i++) { if (cnt <= 5) { if (i == 0)cout << "去书库数书的有:22,26,20,"; if (not_already(random_vec[i])) { cout << random_vec[i]; already.push_back(random_vec[i]); string output = (cnt != 5) ? "," : "\n*如果没通知,12点05宿舍集合\n"; cout << output; ++cnt; } } else if (cnt <= 8) { if (cnt == 6 && index == 0) { cout << "\n宿舍围合接应的有:27,"; ++index; } if (not_already(random_vec[i])) { cout << random_vec[i]; already.push_back(random_vec[i]); string output = (cnt != 8) ? "," : "\n*收到我通知到宿舍门口集合\n"; cout << output; ++cnt; } } } return 0; }

随机数表 or not

目标,输出三个-20到60的随机数和它们的平均数。

Example 1(实际就是随机数表)

#include <iostream> #include <cstdlib> using namespace std; int main() { int a = rand() % 81 - 20; // 除81的余数从0到80 int b = rand() % 81 - 20; int c = rand() % 81 - 20; cout << a << " " << b << " " << c << endl; // 这句可以看实际a,b,c的值 cout << static_cast<double>(a + b + c) / 3 << endl; //先强制转成小数,否则是带余除法 return 0; }

输出示例: 每遍结果是一样的。

Example 2(不是随机数表)

#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(static_cast<unsigned>(time(NULL))); int a = rand() % 81 - 20; // 除81的余数从0到80 int b = rand() % 81 - 20; int c = rand() % 81 - 20; cout << a << " " << b << " " << c << endl; // 这句可以看实际a,b,c的值 cout << static_cast<double>(a + b + c) / 3 << endl; //先强制转成小数,否则是带余除法 return 0; }

输出示例: 改成这样就是根据时间的随机数,而每一遍时间是不一样的,因而随机数就不一样了。


ALL RIGHTS RESERVED © 2020 Teddy van Jerry 欢迎转载,转载请注明出处。


See also

Teddy van Jerry 的导航页 【C++ 程序】 井字棋游戏(人 VS 人) 【C++ 程序】 井字棋游戏(人 VS Lv1电脑) 【C++ 程序】 井字棋游戏(人 VS Lv2电脑) 【C++ 程序】 井字棋游戏(人 VS Lv3电脑) 【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版) 【C++ 程序】 五子棋游戏(人 VS 人) 【C++ 程序】 移动迷宫游戏 【C++ 程序】 贪吃蛇游戏 【C++ 程序】 数字推盘游戏(15-puzzle) 【C++ 程序】 2048游戏 【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面) 【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面) 【C++ 程序】 2048游戏(EasyX 图形界面) 【C++ 程序】 贪吃蛇游戏(EasyX 图形界面)

最新回复(0)