Fluent UDF中使用随机函数
如下给出了调用C语言或Fluent中自带随机函数的例子。利用VC++ UDF Studio插件编译通过(https://vcudfstudio.github.io)。
//利用VC++ UDF Studio插件编译通过 #include "udf.h" #include "stdio.h" #include "time.h" extern "C" { #include "random.h" }; real gaussrand() { static double U, V; static int phase = 0; real Z; if(phase == 0) { U = rand() / (RAND_MAX + 1.0); V = rand() / (RAND_MAX + 1.0); Z = sqrt(-2.0 * log(U))* sin(2.0 * M_PI * V); } else { Z = sqrt(-2.0 * log(U)) * cos(2.0 * M_PI * V); } phase = 1 - phase; return Z; } DEFINE_ON_DEMAND(test) { srand((unsigned)time(NULL)); for (int i = 0; i < 5; i++) { Message("C/C++ uniform random,[0,1]=%f, [3,7]=%d\n", 1.0*rand()/RAND_MAX,rand()%5+3); //C语言均匀随机函数 } for (int i = 0; i < 5; i++) { Message("C/C++ gauss random=%f\n", gaussrand()); //C语言高斯随机函数 } for (int i = 0; i < 5; i++) { Message("Fluent uniform random=%f,cheap random=%f\n", uniform_random(),cheap_uniform_random()); //fluent中定义的均匀随机函数 } for (int i = 0; i < 5; i++) { Message("Fluent gauss random=%f,cheap random=%f\n", gauss_random(), cheap_gauss_random()); //fluent中定义的高斯随机函数 } }
运行结果如下:


浙公网安备 33010602011771号