产生在球面上均匀分布的点

这篇博客探讨了在球面上生成均匀分布点的多种方法,包括:通过球坐标生成,三维球面上的Marsaglia方法,直接抽样法,以及力学方法。虽然简单的方法如生成角度分布的点在两极会密集,但Marsaglia方法能实现360°无死角的均匀分布。此外,力学方法利用粒子间的相互排斥达到最终的均匀状态。
摘要由CSDN通过智能技术生成

如何产生在球面上均匀分布的点呢? 这里提供若干种思路。

1. 生成两个随机分布的角度,并且按照球坐标的形式产生。

缺点: 产生的点是按照角度均匀分布的, 但是注意这并不是球面上均匀分布的点,后面可以看到两极的地方角度明显密集。并且,由于在计算过程中有大量的三角函数的计算,程序的效率不高。 

 要求不高时可以采用这种方法。

思路是,采用10807 方法产生一组两个随机数,分别作为角度使用。将产生的随机数输出到plt 文件中,使用tecplot绘图。(tecplot是非常好用的CFD后处理可视化的软件,强烈推荐使用)。关于10807 方法产生随机数,有空就另外再开一篇帖子。

下面附上 C++  代码:

 1 #include <iostream>
 2 #include<cmath>
 3 #include<fstream>
 4 
 5 using namespace std;
 6 
 7 class cRandom
 8 {
 9     public:
10         cRandom(int x,double y):seed(x),random(y){};
11         cRandom():seed(0),random(0){};
12 
13         int seed;
14         double random;
15 };
16 
17 cRandom my_random(int z)
18 // 16807 way to create random numbers
19 // z is the seed number, num is the total random number to create
20 {
21     //z(n+1)=(a*z(n)+b) mod m
22     //describe m=a*q+r to avoid that the number is large than the computer can bear
23     const int m=pow(2,31)-1;
24     const int a=16807;
25     const int q=127773;
26     const int r=2836;
27 
28     int temp=a*(z%q)-r*(z/q);
29 
30     if(temp<0)
31     {
32         temp=m+temp;
33     }
34     //z is the seed number
35     z=temp;
36     double t = z*1.0/m;
37 
38     cRandom cr;
39     cr.random=t;
40     cr.seed=z;
41 
42     return cr;
43 }
44 
45 int main()
46 {
47     cout << "Hello world!" << endl;
48     const int num = pow(10,5);
49     int z1 = 20;
50     int z2=112;
51 
52     ofstream fcout;
53     fcout.open("result.plt");
54     fcout<<" title=random  "<<endl;
55     fcout<<"  VARIABLES = \"X\",\"Y \",\"Z \" "<<endl;
56     fcout<<"zone I= "<<num<<",datapacking=POINT"<<endl;
57     cRandom sita(z1,0.0);
58     cRandom pesi(z2,0.0);
59     const double pi = 3.141592;
60 
61     for(int i=0;i!=num;++i)
62     {
63         sita=my_random(pesi.seed);
64         pesi=my_random(sita.seed);
65 
66         double x=1.0*sin(pi*sita.random)*cos(2*pi*pesi.random);
67         double y=1.0*sin(pi*sita.random)*sin(
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值