Fluent UDF中使用随机函数

如下给出了调用C语言或Fluent中自带随机函数的例子。利用VC++ UDF Studio插件编译通过。

//利用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++)
	{		
//fluent中定义的均匀随机函数	
		Message("Fluent uniform random=%f,cheap random=%f\n", uniform_random(),cheap_uniform_random());	
	}	

	for (int i = 0; i < 5; i++)
	{
//fluent中定义的高斯随机函数
		Message("Fluent gauss random=%f,cheap random=%f\n", gauss_random(), cheap_gauss_random());
	}
}

运行结果如下:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值