annotate Engine/Random.cpp @ 2542:bacf9809126a

.
author a.parshin
date Sun, 10 May 2015 02:04:52 +0200
parents 68cdef6879a0
children c674d547cc7c
rev   line source
2499
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
1 #define _CRTDBG_MAP_ALLOC
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
2 #include <stdlib.h>
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
3 #include <crtdbg.h>
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
4
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
5 #define _CRT_SECURE_NO_WARNINGS
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
6 #include <math.h>
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
7
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
8 #include "Random.h"
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
9 #include "OurMath.h"
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
10
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
11
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
12
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
13
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
14 struct Random *pRnd=new Random(); // idb
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
15
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
16
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
17
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
18
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
19
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
20 //----- (004BE59E) --------------------------------------------------------
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
21 void Random::SetRange(int min, int max)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
22 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
23 this->min = min;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
24 this->range = max - min;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
25 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
26
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
27 //----- (004BE5B1) --------------------------------------------------------
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
28 double Random::GetRandom()
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
29 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
30 Random *v1; // esi@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
31 double v2; // st7@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
32 double v3; // st7@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
33
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
34 v1 = this;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
35 this->field_0[3] = this->field_0[2];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
36 this->field_0[2] = this->field_0[1];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
37 v2 = this->field_0[0];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
38 this->field_0[1] = v2;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
39 v3 = this->field_0[3] * 1492.0
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
40 + this->field_0[2] * 1776.0
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
41 + this->field_0[3] * 2111111111.0
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
42 + v2 * 5115.0
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
43 + this->field_0[4];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
44 v1->field_0[0] = v3 - floor(v3);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
45 v1->field_0[4] = floor(v3) * 2.328306436538696e-10;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
46 return v1->field_0[0];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
47 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
48
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
49 //----- (004BE623) --------------------------------------------------------
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
50 int Random::GetInRange()
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
51 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
52 // Random *v1; // esi@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
53 float v2; // ST10_4@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
54 // double v3; // ST04_8@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
55 int floored_random; // ecx@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
56
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
57 v2 = GetRandom() * (double)this->range;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
58 floored_random = bankersRounding(v2);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
59 if ( floored_random >= this->range )
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
60 floored_random = this->range;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
61 return floored_random + this->min;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
62 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
63
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
64 //----- (004BE65D) --------------------------------------------------------
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
65 void Random::Initialize(int seed)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
66 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
67 int v2; // eax@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
68 Random *this_; // esi@1
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
69 signed int v4; // ecx@3
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
70 unsigned int v5; // eax@4
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
71 signed int v6; // edi@5
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
72
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
73 v2 = seed;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
74 this_ = this;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
75 if ( !seed )
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
76 v2 = -1;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
77 v4 = 0;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
78 do
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
79 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
80 v5 = (((v2 << 13) ^ (unsigned int)v2) >> 17) ^ (v2 << 13) ^ v2;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
81 v2 = 32 * v5 ^ v5;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
82 this_->field_0[v4++] = (double)(unsigned int)v2 * 2.328306436538696e-10;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
83 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
84 while ( v4 < 5 );
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
85 v6 = 19;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
86 do
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
87 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
88 GetRandom();
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
89 --v6;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
90 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
91 while ( v6 );
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
92 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
93