-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbinormal.c
58 lines (46 loc) · 1.19 KB
/
binormal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/***********************************************************
binormal.c -- 2変量正規分布
***********************************************************/
/***** 一様乱数 (線形合同法) ******************************/
#include <limits.h>
static unsigned long seed = 1; /* 任意 */
void init_rnd(unsigned long x)
{
seed = x;
}
unsigned long irnd(void)
{
seed = seed * 1566083941UL + 1;
return seed;
}
double rnd(void) /* 0 <= rnd() < 1 */
{
return (1.0 / (ULONG_MAX + 1.0)) * irnd();
}
/**********************************************************/
void binormal_rnd(double r, double *x, double *y) /* 2変量正規分布の乱数 */
{
double r1, r2, s;
do {
r1 = 2 * rnd() - 1;
r2 = 2 * rnd() - 1;
s = r1 * r1 + r2 * r2;
} while (s > 1 || s == 0);
s = - log(s) / s;
r1 = sqrt((1 + r) * s) * r1;
r2 = sqrt((1 - r) * s) * r2;
*x = r1 + r2; *y = r1 - r2;
}
/**********************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
double x, y;
for (i = 0; i < 20; i++) {
binormal_rnd(0.5, &x, &y);
printf("%10.5f %10.5f\n", x, y);
}
return 0;
}