欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++生成隨機(jī)浮點(diǎn)數(shù)的示例代碼

 更新時(shí)間:2022年04月14日 15:30:20   作者:叫我小秦就好了  
在C++11之前,我們通常采用rand函數(shù)來(lái)生成隨機(jī)數(shù),但rand函數(shù)對(duì)一些情況顯得難以處理。本文將介紹如何利用C++生成隨機(jī)浮點(diǎn)數(shù),需要的可以參考一下

前言

在C++11之前,我們通常采用rand函數(shù)來(lái)生成隨機(jī)數(shù)。

但rand函數(shù)對(duì)一些情況顯得難以處理:

  • 不同范圍的隨機(jī)數(shù)
  • 需要隨機(jī)浮點(diǎn)數(shù)
  • 需要非均勻分布的隨機(jī)數(shù)

rand生成隨機(jī)數(shù)

問(wèn)題出現(xiàn)

場(chǎng)景描述:

想生成一組整形隨機(jī)數(shù),放入數(shù)組中,用來(lái)測(cè)試自己的排序是否正確。

于是我寫(xiě)出了下方代碼,生成隨機(jī)數(shù)。

先簡(jiǎn)單了解下用到的函數(shù):

//返回time_t類(lèi)型的 當(dāng)前時(shí)間的時(shí)間戳
time_t time (time_t* timer);

//傳入一個(gè)種子,為偽隨機(jī)數(shù)生成器初始化
void srand (unsigned int seed);

//得到一個(gè)整形偽隨機(jī)數(shù)
int rand (void);
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    int arr[10] = { 0 };
    
    for (int i = 0; i < 10; ++i)
    {
        srand((unsigned int)time(NULL));
        //兩個(gè)相減是為了出現(xiàn)負(fù)的隨機(jī)數(shù),使測(cè)試范圍更廣
        arr[i] = (rand() % 100 + 1) - (rand() % 100 + 1);
        
        printf("%d ", arr[i]);
    }
    
    return 0;
}

我發(fā)現(xiàn)盡管我調(diào)用了srand函數(shù),可生成的數(shù)組值還是同一個(gè)。

我思考后想到,因?yàn)閒or循環(huán)執(zhí)行速度太快,整個(gè)程序都是在一秒內(nèi)完成的。

所以出現(xiàn)了都是同一個(gè)值的情況。

初步解決

于是我想出了下面的解決方法:

我可以在for循環(huán)內(nèi)調(diào)用Sleep函數(shù),讓我的電腦休眠一下,這樣就不會(huì)出現(xiàn)上述情況了。

于是我寫(xiě)出了下方的代碼:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>


int main()
{
    int arr[10] = { 0 };

    for (int i = 0; i < 10; ++i)
    {
        Sleep(1000);
        srand((unsigned int)time(NULL));
        arr[i] = (rand() % 100 + 1) - (rand() % 100 + 1);

        printf("%d ", arr[i]);
    }

    return 0;
}

通過(guò)休眠后,就成功解決問(wèn)題了。

可是,

如果睡眠時(shí)間太短,那么還是會(huì)出現(xiàn)重復(fù)的現(xiàn)象;

如果睡眠時(shí)間太長(zhǎng),程序運(yùn)行速度就太慢。

最終方法

因?yàn)樯鲜龅脑?,我繼續(xù)查詢資料,了解了rand和srand的基本原理,最終成功解決了該問(wèn)題。

給srand函數(shù)傳入一個(gè)數(shù)值后,srand會(huì)根據(jù)這個(gè)生成一個(gè)隨機(jī)序列表(通常有4,294,967,296個(gè)數(shù)),傳入相同的數(shù)生成的序列表是相同的。然后rand從序列的頭部取出一個(gè)數(shù)返回,然后將這個(gè)數(shù)放在隨機(jī)序列表尾部,因此如果你要取的數(shù)據(jù)量非常大,是會(huì)出現(xiàn)與之前取出的數(shù)重復(fù)的情況。

此時(shí),上面出現(xiàn)的問(wèn)題也很好解決了。因?yàn)橛?jì)算機(jī)運(yùn)行速度很快,所以我們每次進(jìn)入循環(huán)都會(huì)生成一個(gè)相同的隨機(jī)序列表,rand函數(shù)只會(huì)取出其第一個(gè)數(shù)。

要解決這個(gè)問(wèn)題,我們只需要在循環(huán)前調(diào)用一次srand函數(shù)就好了,這樣就不會(huì)重復(fù)生成序列表了。

下方是最終形式的代碼:

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

int main()
{
    int arr[10] = { 0 };

    srand((unsigned int)time(NULL));
    for (int i = 0; i < 10; ++i)
    {
        arr[i] = (rand() % 100 + 1) - (rand() % 100 + 1);

        printf("%d ", arr[i]);
    }

    return 0;
}

下文將使用C++11定義在頭文件random中的隨機(jī)數(shù)庫(kù)通過(guò)一組協(xié)作的類(lèi)來(lái)解決這些問(wèn)題:隨機(jī)數(shù)引擎類(lèi)隨機(jī)數(shù)分布類(lèi)

  • 一個(gè)引擎類(lèi)可以生成unsigned隨機(jī)數(shù)序列
  • 一個(gè)分布類(lèi)使用一個(gè)引擎類(lèi)生成指定類(lèi)型的、在給定范圍內(nèi)的、服從特定概率分布的隨機(jī)數(shù)

生成等概率隨機(jī)數(shù)

生成隨機(jī)整數(shù)

uniform_int_distribution:產(chǎn)生均勻分布的整數(shù)

template <class IntType = int> 
class uniform_int_distribution;

// IntType
// An integer type. Aliased as member type result_type.
// By default, this is int.
#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
    //產(chǎn)生[1, 100]左閉右閉區(qū)間的隨機(jī)整數(shù)
	uniform_int_distribution<int> u(1, 100);
	default_random_engine e;
    //為隨機(jī)數(shù)引擎設(shè)置隨機(jī)種子,若不設(shè)置每次生成的隨機(jī)數(shù)相同(可以創(chuàng)建時(shí)設(shè)置)
    //類(lèi)似srand的用法,相同的種子生成的隨機(jī)數(shù)相同
    //default_random_engine e(time(NULL));
    e.seed(time(NULL));

	for (size_t i = 0; i < 10; ++i)
	{
		cout << u(e) << " ";
	}
	cout << endl;

	return 0;
}

生成隨機(jī)浮點(diǎn)數(shù)

uniform_real_distribution:產(chǎn)生均勻分布的實(shí)數(shù)

template <class RealType = double> 
class uniform_real_distribution;

// RealType
// A floating-point type. Aliased as member type result_type.
// By default, this is double.
#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
    //生成[-1, 1]范圍隨機(jī)浮點(diǎn)數(shù)
    //模板參數(shù)只能是浮點(diǎn)類(lèi)型(float,double, long double)
	uniform_real_distribution<double> u(-1, 1);
    default_random_engine e(time(NULL));

	for (size_t i = 0; i < 10; ++i)
	{
		cout << u(e) << " ";
	}
	cout << endl;
    
	return 0;
}

生成非均勻分布隨機(jī)數(shù)

正態(tài)分布隨機(jī)數(shù)

template <class RealType = double> 
class normal_distribution;
#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
    //生成符合均值為10,標(biāo)準(zhǔn)差為2的隨機(jī)數(shù)
	normal_distribution<double> u(10, 2);
    default_random_engine e(time(NULL));
    
	for (size_t i = 1; i <= 100; ++i)
	{
		printf("%-9.6lf ", u(e));
		if (i % 10 == 0)
		{
			cout << endl;
		}
	}
    cout << endl;

	return 0;
}

二項(xiàng)分布的布爾值

class bernoulli_distribution;
#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
	// 生成1的概率為0.7
	bernoulli_distribution u(0.7); 
    default_random_engine e(time(NULL));

	for (int i = 0; i < 10; i++) {
		cout << u(e) << " ";
	}
    cout << endl;

	return 0;
}

以上就是C++生成隨機(jī)浮點(diǎn)數(shù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++隨機(jī)浮點(diǎn)數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論