【发布时间】:2011-06-26 07:19:00
【问题描述】:
各位stackoverflow的各位朋友大家好,最近刚开始学习C++,今天写了一个小游戏,但是随机函数不能正常工作。当我多次调用我的随机函数时,它不会重新生成一个数字,而是一遍又一遍地打印相同的数字。如何在不使用 for 循环的情况下解决此问题? 谢谢
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int rolld6();
int main()
{
cout<<rolld6()<<endl;
cout<<rolld6()<<endl;
system("PAUSE");
return 0;
}
int rolld6()
{
srand(time(NULL));
return rand() % 6 + 1;;
}
【问题讨论】: