我尝试着创造一款带有骰子的游戏,我需要在其中添加一些随机数字(游戏邦注:即模拟骰子的边缘)。我知道如何在1到6之间
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
int i;
i = (rand()%6)+1;
cout << i << "\n";
}
并不是很好地工作,因为当我运行程序几次,这是我得到的输出:
6
1
1
1
1
1
2
2
2
2
5
2
所以我想要一个每次生成不同随机数的命令,而不是连续5次生成相同的随机数。是否有这样的命令?
非常固执己见的回答
c++ <random>库违反了软件工程的最佳原则之一:“简单的事情做简单,复杂的事情,不寻常的事情可以更复杂一点。”
相反,他们甚至把简单和常见的用例变得过于复杂,只是因为他们患有文化疾病,害怕像“这还不够普遍”这样的评论。
As a result, now whenever you want a simple random number, you have to look into the documentation, read stack overflow with walls of text, glorifying this terrible design, instead of it just being an easy-to-remember one or 2 liner. (Common Lisp is more pragmatic: (random 5) yields uniformly distributed integers from 0..4 and (random 1.0) yields real numbers between 0.0..1.0. That is the most common use case and it is at your finger tips. If you need more sophisticated stuff, you have to find packages and libraries or do it yourself.)
只需计算一下全球范围内每个人浪费在理解标题及其内容上的时间累积的工时,就可以看到它有多糟糕。
即使我现在在浪费我的时间,写这个答案,你也在浪费时间,阅读它,只是因为他们创造了一个复杂的谜题,这与其他现代令人厌恶的东西相似,比如Vulkan API。
那么,如何应对呢?浪费一次时间,为自己最常见的用例编写一个头文件,然后在需要时重用它。