-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolargen_e.cpp
47 lines (42 loc) · 1009 Bytes
/
polargen_e.cpp
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
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
class PolarGen{
public:
PolarGen(){
nincsTarolt = true;
std::srand(std::time(NULL));
}
~PolarGen(){}
double kovetkezo(){
if(nincsTarolt){
double u1, u2, v1, v2, w;
do {
u1 = std::rand() / (RAND_MAX + 1.0);
u2 = std::rand() / (RAND_MAX + 1.0);
v1 = 2 * u1 -1;
v2 = 2 * u1 -1;
w = v1 *v1 + v2 * v2;
}
while(w > 1);
double r = std::sqrt((-2 * std::log(w)) / w);
tarolt = r * v2;
nincsTarolt = !nincsTarolt;
return r * v1;
}
else {
nincsTarolt = !nincsTarolt;
return tarolt;
}
}
private:
bool nincsTarolt;
double tarolt;
};
int main(){
PolarGen pg;
for(int i = 0; i < 10; ++i)
std::cout << pg.kovetkezo() << std::endl;
return 0;
}