C++ VTK實(shí)例之高斯隨機(jī)數(shù)的生成
VTK實(shí)例高斯隨機(jī)數(shù)的生成
這個(gè)例子演示了從一個(gè)平均數(shù)是0.0和標(biāo)準(zhǔn)偏差是2.2的高斯分布中隨機(jī)生成3個(gè)隨機(jī)數(shù)。先創(chuàng)建兩個(gè)文件:GaussianRandomNumber.cpp和CMakeLists.txt。
代碼如下:
GaussianRandomNumber.cxx
#include <vtkBoxMuellerRandomSequence.h> #include <vtkNew.h> int main(int, char*[]) { // The number of random numbers we wish to produce unsigned int numRand = 3; vtkNew<vtkBoxMuellerRandomSequence> randomSequence; // Generate numRand random numbers from a Gaussian distribution with mean 0.0 // and standard deviation 2.2 auto mean = 0.0; auto standardDeviation = 2.2; for (unsigned int i = 0; i < numRand; i++) { auto a = randomSequence->GetScaledValue(mean, standardDeviation); randomSequence->Next(); std::cout << a << std::endl; } return EXIT_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(GaussianRandomNumber) find_package(VTK COMPONENTS vtkCommonCore QUIET ) if (NOT VTK_FOUND) message("Skipping GaussianRandomNumber: ${VTK_NOT_FOUND_MESSAGE}") return () endif() message (STATUS "VTK_VERSION: ${VTK_VERSION}") if (VTK_VERSION VERSION_LESS "8.90.0") # old system include(${VTK_USE_FILE}) add_executable(GaussianRandomNumber MACOSX_BUNDLE GaussianRandomNumber.cxx ) target_link_libraries(GaussianRandomNumber PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(GaussianRandomNumber MACOSX_BUNDLE GaussianRandomNumber.cxx ) target_link_libraries(GaussianRandomNumber PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS GaussianRandomNumber MODULES ${VTK_LIBRARIES} ) endif ()
編譯
將以上兩個(gè)文件放在自定義文件夾里,再新建一個(gè)build目錄,用于存放編譯后生成的文件。打開CMake程序,編譯設(shè)置界面如下:
調(diào)試
在剛剛生成的build目錄下,以管理員身份打開vs2019,再打開.sln文件,點(diǎn)擊build生成解決方案,如下圖:
并將GaussianRandomNumber設(shè)為啟動(dòng)項(xiàng),
進(jìn)行本地調(diào)試,生成三個(gè)隨機(jī)數(shù):
//注:確保將VTK bin目錄添加到路徑中,這將在運(yùn)行時(shí)解析VTK 的dll文件。
到此這篇關(guān)于C++ VTK實(shí)例之高斯隨機(jī)數(shù)的生成的文章就介紹到這了,更多相關(guān)C++ 的內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VC++簡(jiǎn)單實(shí)現(xiàn)關(guān)機(jī)、重啟計(jì)算機(jī)實(shí)例代碼
這篇文章主要介紹了VC++簡(jiǎn)單實(shí)現(xiàn)關(guān)機(jī)、重啟計(jì)算機(jī)實(shí)例代碼,很實(shí)用的功能,需要的朋友可以參考下2014-07-07