本文介紹了在 C++ 中生成隨機(jī)雙數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如何在 C++ 中生成兩個雙精度數(shù)之間的隨機(jī)數(shù),這些數(shù)字應(yīng)該看起來像 xxxxx,yyyyy .
How to generate random numbers between two doubles in c++ , these numbers should look like xxxxx,yyyyy .
推薦答案
方法如下
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
記住在每次程序啟動時使用適當(dāng)?shù)姆N子調(diào)用 srand().
Remember to call srand() with a proper seed each time your program starts.
這個答案已經(jīng)過時了,因為 C++ 得到了它的原生非基于 C 的隨機(jī)庫(參見 Alessandro Jacopsons 答案)但是,這仍然適用于 C
This answer is obsolete since C++ got it's native non-C based random library (see Alessandro Jacopsons answer) But, this still applies to C
這篇關(guān)于在 C++ 中生成隨機(jī)雙數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!