Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace lehmer
- {
- class CLehmerGenerator
- {
- private UInt64 m_a;
- private UInt64 m_m;
- private UInt64 m_c;
- private UInt64 m_X;
- private UInt64 m_Xo;
- private UInt64 maxCapacity = 1000000;
- private UInt64 curCapacity = 1000000;
- public CLehmerGenerator(UInt64 a, UInt64 m, UInt64 c, UInt64 Xo)
- {
- m_a = a;
- m_m = m;
- m_c = c;
- m_X = Xo;
- m_Xo = Xo;
- }
- public UInt64 GetX()
- {
- curCapacity--;
- return m_X = (m_a * m_X + m_c) % m_m;
- }
- public double GetValue()
- {
- return ((double)GetX()) / m_m;
- }
- public UInt64 GetCapacity
- {
- get { return curCapacity; }
- }
- public void Reset()
- {
- m_X = m_Xo;
- curCapacity = maxCapacity;
- }
- public UInt64 GetM()
- {
- return this.m_m;
- }
- }
- class Criterions
- {
- public static double A(CLehmerGenerator example)
- {
- example.Reset();
- int[] d = new int[10];
- for (int i = 0; i < 1000000; i++)
- {
- int y = (int)Math.Floor(10 * example.GetValue());
- d[y]++;
- }
- double v = 0;
- for (int j = 0; j < 10; j++)
- {
- v +=( Math.Pow(d[j] - 1000000 * 0.1, 2.0) )/ (0.1 * 1000000);
- }
- return v;
- }
- public static double B(CLehmerGenerator example)
- {
- example.Reset();
- int[,] d = new int[10, 10];
- for (int i = 0; i < 500000; i++)
- {
- int y1 = (int)Math.Floor(10 * example.GetValue());
- int y2 = (int)Math.Floor(10 * example.GetValue());
- d[y1,y2]++;
- }
- double v = 0;
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- v += (Math.Pow(d[i, j] - 500000 * 0.01, 2.0)) / (0.01 * 500000);
- }
- }
- return v;
- }
- public static double C(CLehmerGenerator example)
- {
- UInt64 count = 0;
- int start = 0;
- UInt64 k = 1000000;
- double sa = 0;
- double sb = 0.5;
- int segmSize = 10;
- int[] segm = new int[segmSize];
- double v = 0;
- double p = (sb - sa);
- double prt = 1;
- for (UInt64 i = 0; i < k; i++)
- {
- double ch = example.GetValue();
- if (sa <= ch && ch <= sb && count < example.GetM())
- {
- if (start < segmSize - 1)
- {
- segm[start]++;
- }
- else
- {
- segm[segmSize - 1]++;
- }
- start++;
- }
- else
- {
- count++;
- start = 0;
- }
- }
- for (int i = 0; i < (int)(segmSize - 1); i++)
- {
- v += ((segm[i] - p * prt * count) * (segm[i] - p * prt * count)) / (p * prt * count);
- prt *= (1 - p);
- }
- v += (segm[segmSize - 1] - (1 - p) * prt * count) * (segm[segmSize - 1] - (1 - p) * prt * count) / ((1 - p) * prt * count);
- return v;
- }
- //public static double D(CLehmerGenerator example)
- //{
- //}
- }
- class Coin
- {
- public enum OrResh
- {
- орел,
- решка
- };
- CLehmerGenerator k;
- public Coin()
- {
- k = new CLehmerGenerator(40967, 7140251, 1273, 5);
- }
- public OrResh Throw()
- {
- double m = k.GetValue()%100000;
- if (m < 0.5)
- {
- return OrResh.орел;
- }
- else
- {
- return OrResh.решка;
- }
- }
- }
- class ProhProcess
- {
- int CurrentState;
- public Coin c;
- public ProhProcess(Coin c)
- {
- this.c = c;
- CurrentState = 0;
- }
- public int GetValue()
- {
- return CurrentState;
- }
- public void Next()
- {
- Coin.OrResh res = this.c.Throw();
- if (res == Coin.OrResh.орел)
- {
- CurrentState++;
- }
- }
- }
- class Program
- {
- UInt64 factorial(UInt64 n)
- {
- if (n == 1)
- {
- return 1;
- }
- else
- {
- return n * factorial(n - 1);
- }
- }
- UInt64 Combinative(UInt64 n, UInt64 k)
- {
- return factorial(n) / (factorial(k) * factorial(n - k));
- }
- UInt64 Probability(UInt64 n, UInt64 k)
- {
- return Combinative(n, k) * (UInt64)Math.Pow(0.5, k) * (UInt64)Math.Pow(0.5, n - k);
- }
- static void Main(string[] args)
- {
- CLehmerGenerator exapmle = new CLehmerGenerator(40967, 7140251, 1273, 5);
- Console.WriteLine(Criterions.A(exapmle));
- Console.WriteLine(Criterions.B(exapmle));
- Console.WriteLine(Criterions.C(exapmle));
- double[] n = new double[10];
- double[,] p = new double[10, 10];
- double[,] Prob = new double[10, 10];
- for (UInt64 j = 0; j < 2000000; j++)
- {
- Coin c = new Coin();
- ProhProcess example = new ProhProcess(c);
- for (int i = 0; i < 20; i++)
- {
- int cur = example.GetValue();
- ///Console.Write("{0} ", cur);
- example.Next();
- int next = example.GetValue();
- if ((next < 10) && (cur < 10))
- {
- n[next]++;
- p[cur, next]++;
- }
- }
- //Console.WriteLine();
- }
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- Prob[i, j] = p[i, j] / n[i];
- }
- }
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- Console.Write("{0,4:f2} ", Prob[i, j]);
- }
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement