小编Man*_*dey的帖子

C++中的rand()和srand()

在C++中生成随机数的基础是什么?

那背后有一些逻辑或原理吗?

数字是否完全随机生成?

假设我正在运行此程序:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    /*
    Declare variable to hold seconds on clock.
    */
    time_t seconds;
    /*
    Get value from system clock and
    place in seconds variable.
    */
    time(&seconds);
    /*
    Convert seconds to a unsigned
    integer.
    */
    srand((unsigned int) seconds);
    /*
    Output random values.
    */
    cout<< rand() << endl;
    cout<< rand() << endl;
    cout<< rand() << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它显示的内容:http: //img14.imageshack.us/img14/1538/98271820.png

它显示205两次.

c++ random

8
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×1

random ×1