C++ random numbers

C++ random numbers

本文关键字:numbers random C++      更新时间:2023-10-16

如何在Linux和C++中使用随机数?

我找到了一些我想使用的代码,它有行

srand((unsigned)time(0));//seed

但是gcc说

board.cpp:94:24:错误:未在此作用域中声明"time"

我已经包括以下文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <algorithm>

您需要

#include <ctime>

以访问CCD_ 1功能。

您还没有包含定义函数的头,为了让程序知道如何调用time(),您需要添加以下内容:

#include <time.h>

为了挑剔(代码风格),您的调用应该更正确地为时间(NULL),而不是时间(0)

您需要包括<time.h>

事实上,它错过了时间函数try:的include

#include <time.h>

你可以去那里了解更多关于srand:的解释

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/