簡體   English   中英

C ++隨機數

[英]C++ random numbers

如何在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>

訪問time功能。

您沒有包含定義函數的標題,以便您的程序知道如何調用time(),您需要添加以下內容:

#include <time.h>

並且只是為了挑剔(代碼風格)你的調用應該更准確地是時間(NULL)而不是時間(0)

您需要包含<time.h>

確實,它錯過了時間函數的包含嘗試:

#include <time.h>

你可以去那里對srand有更多的解釋:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM