如何在 c++ 中生成 0 到 4 之间的随机数
How can I generate a random number between 0 and 4 in c++?
我想生成一个介于 0 和 4可能的重复项:
如何从范围内生成随机数 - C
之间的随机数(包括 0 和 4)。 如何在 c++ 中执行此操作?
你可以调用 std::rand()
,使用取模运算符将范围限制为所需的范围。
std::rand()%5
您还可以查看新的 C++11 随机数生成实用程序
为了完整起见,我向您展示了一个使用新random
工具的 C++11 解决方案:
#include <random>
#include <ctime>
int main() {
std::minstd_rand generator(std::time(0));
std::uniform_int_distribution<> dist(0, 4);
int nextRandomInt = dist(generator);
return 0;
}
相关文章:
- 查找彼此之间具有不同最小距离的随机数
- 使用 std::binomial_distribution 在范围之间生成随机数
- 如何在 C++ 中获取 5 到 -5 之间的随机数
- C++ 和 Python 实现之间的不同伪随机数
- 生成随机数,并保证在给定域之间覆盖所有
- 我如何确保我的随机数在兰德生成的0和1之间不是0
- 在[0,1)C 之间生成一个随机数
- 0-1之间的随机数生成以填充容器
- 盒子muller随机数在C 中的0到1之间
- 在random_device和seed_seq之间做出决定,为多个随机数序列生成种子
- C++ 中介于 0 和 1 之间的随机数
- 本应生成 2 个介于 1 和 9 之间的随机数的程序C++只生成 6 和 9 并且没有正确循环
- 在0到1之间生成随机数
- 分叉进程之间的随机数是相同的
- 如何在 c++ 中生成 0 到 4 之间的随机数
- 介于 2 个值之间的随机数
- 如何在我存储的两个变量之间生成随机数
- 使用 C++ 的 1 到 10 之间的随机数
- 如何在c++中生成一个介于1和16之间的随机数
- C++生成一个介于 0 和 100,000 之间的随机数