首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

随机一个个位数,然后把个位数strcat到字符串尾部//不知道为什么正确的如何做

2013-12-15 
随机一个个位数,然后把个位数strcat到字符串尾部//不知道为什么正确的怎么做?char numi[8] {}char numr

随机一个个位数,然后把个位数strcat到字符串尾部//不知道为什么正确的怎么做?

char numi[8] = {};
char numr[8] = {};
char c[2] = {};
cin>>numi;
srand((unsigned)time(NULL));
for(int i = 0; i< 7;i++)
{
int nRnd = rand();
strcat(numr,itoa(nRnd,c,10));
memset(c,2,0);
}
[解决办法]
int nRnd = rand() % 10; // 保证你的随机数 nRnd 是个位数。

int rand (void);
Returns a pseudo-random integral number in the range between 0 and RAND_MAX.

RAND_MAX
This value is library-dependent, but is guaranteed to be at least 32767

热点排行