C++疑问,代码中随机数产生的结果都是一样的 ,希望有大佬可以答疑

qq_28848337 2018-04-03 02:14:59
/*利用for循环来说明continue语句的用法,判断其是否会跳过循环的剩余部分。通过while循环累加随机数的值,如果大于100,就退出该循环*/
#include <iostream>
#include "stdafx.h"
using namespace std;
void main(void)
{
cout << "打印1~10之间的技术" << endl;
for (int count = 1; count <= 10; count++)
{
if (count % 2 == 0) //判断是否是偶数
continue; //跳过剩余循环部分
cout << count << " ";
}
cout << endl;
cout << "产生随机数和是:"<<endl;
int max = 0;
int tmp(0);
while (true) //无限循环
{
srand(max); //随机数产生种子
tmp = rand() % 200; //随机数在200内产生
cout << tmp;
max += tmp; //max=max+tmp,随机数进行累加
if (max > 1000)
{
cout << " = " << max << endl;
break; //跳出while 循环
}
else {
cout << "+";
}

}
system("pause");
}


这段代码中,产生随机数并相加,但是每次产生的随机数都相同,请大佬答疑
...全文
801 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wei_cpp 2018-04-04
  • 打赏
  • 举报
回复
srand写在while前面
赵4老师 2018-04-03
  • 打赏
  • 举报
回复
仅供参考:C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/***
*rand.c - random number generator
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
*       Seeds the random number generator with the int given.  Adapted from the
*       BASIC random number generator.
*
*Entry:
*       unsigned seed - seed to seed rand # generator with
*
*Exit:
*       None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
        unsigned int seed
        )
{
        _getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
*       returns a pseudo-random number 0 through 32767.
*
*Entry:
*       None.
*
*Exit:
*       Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
        void
        )
{
        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );
}
自信男孩 2018-04-03
  • 打赏
  • 举报
回复
#include <iostream>
#include "stdafx.h"

using namespace std;

void main(void)
{
    cout << "打印1~10之间的技术" << endl;
    for (int count = 1; count <= 10; count++)
    {
        if (count % 2 == 0) //判断是否是偶数
            continue; //跳过剩余循环部分
        cout << count << "   ";
    }
    cout << endl;
    cout << "产生随机数和是:"<<endl;
    int max = 0;
    int tmp(0);
    srand(max); //随机数产生种子
    while (true) //无限循环
    {
        tmp = rand() % 200; //随机数在200内产生
        cout << tmp;
        max += tmp; //max=max+tmp,随机数进行累加
        if (max > 1000)
        {
            cout << " = " << max << endl;
            break; //跳出while 循环
        }
        else {
            cout << "+";
        }

    }
    system("pause");
}
将srand放在while循环外。参考一下吧 如果随机产生多个数,有两个数一样那是可能的,随机数,也可能是两个数一样的,有这样的概率。但是如果产生的所有随机值都一样,那么应该是种子的问题。
wallesyoyo 2018-04-03
  • 打赏
  • 举报
回复
一句话就能解答你的疑问呀:种子一样,产生的随机数也是一样的。 所以我们经常看到这样的代码 srand(time(NULL)); 就是让每次运行程序的时候种子不一样啊。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧