C++ 中的随机数
我正在创建一个二十一点程序。我想要随机发牌。如果我使用 rand() 函数并将 srand() 初始化为 time(NULL) 那么所有卡片都是相同的。知道可能是什么问题吗?
//Cards.h
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
class Cards
{
private:
int val_seed,suit_seed;
public:
int val[10];
string card[10];
int dealcard(int x)
{
srand();
val_seed=rand()%13 + 2;
suit_seed=rand()%4 + 1;
if(val_seed>=1 && val_seed<=10)
{
if(val_seed==10)
{
card[x]="10";
}
else
{
card[x]=val_seed+48;
}
}
else if(val_seed>10)
{
switch(val_seed)
{
case 11: card[x]="J"; val_seed=10; break;
case 12: card[x]="Q"; val_seed=10; break;
case 13: card[x]="K"; val_seed=10; break;
case 14: card[x]="A"; val_seed=11; break;
}
}
switch(suit_seed)
{
case 1: card[x]+='\3'; break;
case 2: card[x]+='\4'; break;
case 3: card[x]+='\5'; break;
case 4: card[x]+='\6'; break;
}
val[x]=val_seed;
}
};
//main.cpp
#include <cstdlib>
#include <iostream>
#include "Cards.h"
using namespace std;
int main(int argc, char *argv[])
{
Cards k;
for(int a=1; a<=9; a++)
{
k.dealcard(a);
}
for(int e=1; e<=9; e++)
{
cout<<k.card[e]<<"("<<k.val[e]<<")"<<" ";
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
I'm creating a BlackJack Program. I want the cards to be randomly dealt. if i use the rand() function and initialize srand() to time(NULL) then all the cards turn out to be same. Any idea what the problem might be ?
//Cards.h
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
class Cards
{
private:
int val_seed,suit_seed;
public:
int val[10];
string card[10];
int dealcard(int x)
{
srand();
val_seed=rand()%13 + 2;
suit_seed=rand()%4 + 1;
if(val_seed>=1 && val_seed<=10)
{
if(val_seed==10)
{
card[x]="10";
}
else
{
card[x]=val_seed+48;
}
}
else if(val_seed>10)
{
switch(val_seed)
{
case 11: card[x]="J"; val_seed=10; break;
case 12: card[x]="Q"; val_seed=10; break;
case 13: card[x]="K"; val_seed=10; break;
case 14: card[x]="A"; val_seed=11; break;
}
}
switch(suit_seed)
{
case 1: card[x]+='\3'; break;
case 2: card[x]+='\4'; break;
case 3: card[x]+='\5'; break;
case 4: card[x]+='\6'; break;
}
val[x]=val_seed;
}
};
//main.cpp
#include <cstdlib>
#include <iostream>
#include "Cards.h"
using namespace std;
int main(int argc, char *argv[])
{
Cards k;
for(int a=1; a<=9; a++)
{
k.dealcard(a);
}
for(int e=1; e<=9; e++)
{
cout<<k.card[e]<<"("<<k.val[e]<<")"<<" ";
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为数字生成器播种一次。如果您在循环中运行函数,它们的执行速度将快于时间分辨率间隔(因此所有种子都是相同的,因此数字也是相同的)。
在开发应用程序并调试它时,如果您使用相同的已知值为生成器提供种子,将会有所帮助。这意味着连续的调试会话将使用生成器中的相同值。
不要忘记在发布版本中切换回基于 time() 的种子(或使用预处理器来检测它是调试版本还是发布版本并为您执行此操作)。
Seed the number generator ONCE. If you run your function in a loop they will be executed faster than the time resolution interval (so all the seeds are the same and therefore the numbers).
While developing the app and debugging it it will help if you seed the generator with the same, known value. This means successive debugging sessions will use the same values from the generator.
Don't forget to switch back to a time() based seed in your release build (or use the pre-processor to detect if it's a Debug or Release build and do it for you).
请参阅http://www.cplusplus.com/reference/clibrary/cstdlib/srand/< /a>
srand 最好使用唯一值(例如时间)进行初始化,因为计算机不会生成随机数,但实际上是根据预先完成的列表进行工作,这将起点设置得更难以确定。
See http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
srand is best initialised with a unique value, eg, the time, as computers dont generate random numbers but in effect work from a predone list, this sets the start point to be less determinable.
你不想每次使用
rand
时都调用srand
,只需在程序开始时调用一次就可以了。使用时间作为参数调用它还可以确保每次运行程序时都会得到不同的随机序列。
You do not want to call
srand
every time you userand
, just call it once at the start of the program and you will be fine.Calling it with the time as parameter will also make sure you will get a different random sequence each time you run the program.
尽管使用 srand() 存在问题,但您的卡片生成算法是错误的。即使连续生成两张同一张牌的可能性也意味着它是错误的。
您应该拿一副牌(52 或其倍数),洗牌 它使用随机生成的种子,并从洗好的牌组中发牌。
Problems using
srand()
notwithstanding, your card generation algorithm is wrong. Even the possibility of it generating two of the same card in a row means it is wrong.You should take a deck of cards (52 or some multiple thereof), shuffle it using a randomly generated seed, and deal cards from the shuffled deck.
您正在使用 null 播种 srand() ;使用当前时钟时间进行种子,然后重试应该可以解决您的问题。
You are seeding
srand()
with null; seed with current clock time and then retry it should solve your problem.