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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
-
|
|
-
|
|
|
|
|
|
-
!
-
!
-
|
|
-
|
!
|
!
-
!
-
|
|
|
|
|
|
-
|
|
|
|
|
|
!
|
-
!
-
|
-
|
!
|
|
|
!
|
|
!
-
!
-
|
|
-
|
|
!
-
|
-
|
|
|
|
!
!
|
|
!
-
!
-
|
-
|
!
|
|
-
|
!
!
!
| using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Test6 : MonoBehaviour
{
void Start()
{
print("計算で求めた値:" + (10f / (3f + 1f) + 3f / (10f + 1f)));
print("シミュレーション値:"+Sampling(10, 3, 100000));
print("計算で求めた値:" + (100f / (2f + 1f) + 2f / (100f + 1f)));
print("シミュレーション値:" + Sampling(100, 2, 100000));
}
float Sampling(int whiteBall, int blackBall, int loopCount)
{
int rest = 0;
for (int i = 0; i < loopCount; i++)
{
rest += Rest(whiteBall, blackBall);
}
return (float)rest / (float)loopCount;
}
int Rest(int whiteBall, int blackBall,bool debug=false)
{
int whiteCount = 0;
int blackCount = 0;
int pointer = 0;
bool[] quary = ShuffleBall(whiteBall, blackBall);
for (int i = 0; i < quary.Length; i++)
{
pointer++;
if (quary[i] == true) whiteCount++;
else blackCount++;
if (whiteCount >= whiteBall) break;
if (blackCount >= blackBall) break;
}
if (debug)
{
foreach (var item in quary)
{
print(item);
}
print(quary.Length);
print(pointer);
print(quary.Length - pointer);
}
return quary.Length - pointer;
}
bool[] ShuffleBall(int whiteBall, int blackBall)
{
bool[] query = GetBall(whiteBall, blackBall).ToArray();
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < query.Length - 1; i++)
{
int pointer = Random.Range(i + 1, query.Length);
bool temp = query[pointer];
query[pointer] = query[i];
query[i] = temp;
}
}
return query;
}
IEnumerable<bool> GetBall(int whiteBall, int blackBall)
{
for (int i = 0; i < whiteBall; i++)
{
yield return true; }
for (int i = 0; i < blackBall; i++)
{
yield return false; }
}
}
|