Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace lehmer
  7. {
  8. class CLehmerGenerator
  9. {
  10. private UInt64 m_a;
  11. private UInt64 m_m;
  12. private UInt64 m_c;
  13. private UInt64 m_X;
  14. private UInt64 m_Xo;
  15. private UInt64 maxCapacity = 1000000;
  16. private UInt64 curCapacity = 1000000;
  17. public CLehmerGenerator(UInt64 a, UInt64 m, UInt64 c, UInt64 Xo)
  18. {
  19. m_a = a;
  20. m_m = m;
  21. m_c = c;
  22. m_X = Xo;
  23. m_Xo = Xo;
  24. }
  25. public UInt64 GetX()
  26. {
  27. curCapacity--;
  28. return m_X = (m_a * m_X + m_c) % m_m;
  29. }
  30. public double GetValue()
  31. {
  32. return ((double)GetX()) / m_m;
  33. }
  34. public UInt64 GetCapacity
  35. {
  36. get { return curCapacity; }
  37. }
  38.  
  39. public void Reset()
  40. {
  41. m_X = m_Xo;
  42. curCapacity = maxCapacity;
  43. }
  44. public UInt64 GetM()
  45. {
  46. return this.m_m;
  47. }
  48. }
  49. class Criterions
  50. {
  51. public static double A(CLehmerGenerator example)
  52. {
  53. example.Reset();
  54. int[] d = new int[10];
  55. for (int i = 0; i < 1000000; i++)
  56. {
  57. int y = (int)Math.Floor(10 * example.GetValue());
  58. d[y]++;
  59. }
  60. double v = 0;
  61.  
  62. for (int j = 0; j < 10; j++)
  63. {
  64. v +=( Math.Pow(d[j] - 1000000 * 0.1, 2.0) )/ (0.1 * 1000000);
  65. }
  66. return v;
  67. }
  68. public static double B(CLehmerGenerator example)
  69. {
  70. example.Reset();
  71. int[,] d = new int[10, 10];
  72.  
  73. for (int i = 0; i < 500000; i++)
  74. {
  75. int y1 = (int)Math.Floor(10 * example.GetValue());
  76. int y2 = (int)Math.Floor(10 * example.GetValue());
  77. d[y1,y2]++;
  78. }
  79. double v = 0;
  80.  
  81.  
  82. for (int i = 0; i < 10; i++)
  83. {
  84. for (int j = 0; j < 10; j++)
  85. {
  86. v += (Math.Pow(d[i, j] - 500000 * 0.01, 2.0)) / (0.01 * 500000);
  87. }
  88. }
  89. return v;
  90. }
  91.  
  92. public static double C(CLehmerGenerator example)
  93. {
  94. UInt64 count = 0;
  95. int start = 0;
  96.  
  97. UInt64 k = 1000000;
  98.  
  99.  
  100. double sa = 0;
  101. double sb = 0.5;
  102.  
  103. int segmSize = 10;
  104. int[] segm = new int[segmSize];
  105.  
  106. double v = 0;
  107. double p = (sb - sa);
  108. double prt = 1;
  109.  
  110.  
  111. for (UInt64 i = 0; i < k; i++)
  112. {
  113.  
  114. double ch = example.GetValue();
  115.  
  116.  
  117. if (sa <= ch && ch <= sb && count < example.GetM())
  118. {
  119.  
  120. if (start < segmSize - 1)
  121. {
  122. segm[start]++;
  123. }
  124. else
  125. {
  126. segm[segmSize - 1]++;
  127. }
  128.  
  129. start++;
  130. }
  131. else
  132. {
  133. count++;
  134. start = 0;
  135. }
  136.  
  137. }
  138.  
  139.  
  140.  
  141. for (int i = 0; i < (int)(segmSize - 1); i++)
  142. {
  143. v += ((segm[i] - p * prt * count) * (segm[i] - p * prt * count)) / (p * prt * count);
  144. prt *= (1 - p);
  145. }
  146.  
  147. v += (segm[segmSize - 1] - (1 - p) * prt * count) * (segm[segmSize - 1] - (1 - p) * prt * count) / ((1 - p) * prt * count);
  148. return v;
  149. }
  150.  
  151. //public static double D(CLehmerGenerator example)
  152. //{
  153.  
  154. //}
  155.  
  156.  
  157. }
  158.  
  159.  
  160.  
  161. class Coin
  162. {
  163. public enum OrResh
  164. {
  165. орел,
  166. решка
  167. };
  168.  
  169. CLehmerGenerator k;
  170.  
  171. public Coin()
  172. {
  173. k = new CLehmerGenerator(40967, 7140251, 1273, 5);
  174. }
  175.  
  176. public OrResh Throw()
  177. {
  178.  
  179. double m = k.GetValue()%100000;
  180. if (m < 0.5)
  181. {
  182. return OrResh.орел;
  183. }
  184. else
  185. {
  186. return OrResh.решка;
  187. }
  188. }
  189.  
  190. }
  191. class ProhProcess
  192. {
  193.  
  194.  
  195.  
  196. int CurrentState;
  197. public Coin c;
  198.  
  199. public ProhProcess(Coin c)
  200. {
  201. this.c = c;
  202. CurrentState = 0;
  203. }
  204. public int GetValue()
  205. {
  206. return CurrentState;
  207. }
  208. public void Next()
  209. {
  210. Coin.OrResh res = this.c.Throw();
  211. if (res == Coin.OrResh.орел)
  212. {
  213. CurrentState++;
  214. }
  215.  
  216. }
  217.  
  218. }
  219. class Program
  220. {
  221. UInt64 factorial(UInt64 n)
  222. {
  223. if (n == 1)
  224. {
  225. return 1;
  226. }
  227. else
  228. {
  229. return n * factorial(n - 1);
  230. }
  231. }
  232. UInt64 Combinative(UInt64 n, UInt64 k)
  233. {
  234. return factorial(n) / (factorial(k) * factorial(n - k));
  235. }
  236.  
  237. UInt64 Probability(UInt64 n, UInt64 k)
  238. {
  239. return Combinative(n, k) * (UInt64)Math.Pow(0.5, k) * (UInt64)Math.Pow(0.5, n - k);
  240.  
  241. }
  242. static void Main(string[] args)
  243. {
  244.  
  245. CLehmerGenerator exapmle = new CLehmerGenerator(40967, 7140251, 1273, 5);
  246. Console.WriteLine(Criterions.A(exapmle));
  247. Console.WriteLine(Criterions.B(exapmle));
  248. Console.WriteLine(Criterions.C(exapmle));
  249. double[] n = new double[10];
  250. double[,] p = new double[10, 10];
  251. double[,] Prob = new double[10, 10];
  252.  
  253. for (UInt64 j = 0; j < 2000000; j++)
  254. {
  255. Coin c = new Coin();
  256.  
  257. ProhProcess example = new ProhProcess(c);
  258.  
  259. for (int i = 0; i < 20; i++)
  260. {
  261. int cur = example.GetValue();
  262.  
  263. ///Console.Write("{0} ", cur);
  264.  
  265. example.Next();
  266. int next = example.GetValue();
  267. if ((next < 10) && (cur < 10))
  268. {
  269. n[next]++;
  270. p[cur, next]++;
  271. }
  272. }
  273. //Console.WriteLine();
  274. }
  275.  
  276. for (int i = 0; i < 10; i++)
  277. {
  278. for (int j = 0; j < 10; j++)
  279. {
  280. Prob[i, j] = p[i, j] / n[i];
  281. }
  282.  
  283. }
  284.  
  285.  
  286.  
  287. for (int i = 0; i < 10; i++)
  288. {
  289. for (int j = 0; j < 10; j++)
  290. {
  291. Console.Write("{0,4:f2} ", Prob[i, j]);
  292. }
  293. Console.WriteLine();
  294. }
  295.  
  296.  
  297.  
  298. Console.ReadKey();
  299. }
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement