隨機數頻數_遊程檢驗-安全算法面向過程實現方法

在這裏插入圖片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 隨機數頻數_遊程檢驗
{
    
    public partial class Form1 : Form
    {



        public double cephes_erfc(double x)
        {
            const double one_sqrtpi = 0.564189583547756287, rel_error = 1e-12;
            double a = 1, b = x, c = x, d = x * x + 0.5;
            double q1, q2 = b / d, n = 1.0, t;

            if (Math.Abs(x) < 2.2)
                return 1.0 - cephes_erf(x);
            if (x < 0)
                return 2.0 - cephes_erfc(-x);
            do
            {
                t = a * n + b * x;
                a = b;
                b = t;
                t = c * n + d * x;
                c = d;
                d = t;
                n += 0.5;
                q1 = q2;
                q2 = b / d;
            } while (Math.Abs(q1 - q2) / q2 > rel_error);
            return one_sqrtpi * Math.Exp(-x * x) * q2;
        }
        public double cephes_erf(double x2)
        {
            const double two_sqrtpi = 1.128379167095512574, rel_error2 = 1E-12;
            double sum = x2, term = x2, xsqr = x2 * x2;
            int j = 1;

            if (Math.Abs(x2) > 2.2)
                return 1.0 - cephes_erf(x2);
            do
            {
                term *= xsqr / j;
                sum -= term / (2 * j + 1);
                j++;
                term *= xsqr / j;
                sum += term / (2 * j + 1);
                j++;
            } while (Math.Abs(term) / sum > rel_error2);

            return two_sqrtpi * sum;
        }
        public double Caculater_frequency(string str)
        {
            int[] E = new int[200];
            int i = 0;
            int n = str.Length;
            do
            {
                E[i] = Convert.ToInt32(str[i]) - 48;
                i++;
            } while (i < n);
            double  S_n = 0;
            for (i = 0; i < n; i++)
                S_n = (2 * E[i] - 1) + S_n;
            if (S_n < 0)
                S_n = -S_n;
            double S_obs = S_n / Math.Sqrt(n);
            return cephes_erfc(S_obs / Math.Sqrt(2));

        }
        public double Caculater_runs(string str)
        {
            int[] E = new int[200];
            int i = 0;
            int n = str.Length;
            double V_obs = 0;
            int r_i;
            do
            {
                E[i] = Convert.ToInt32(str[i]) - 48;
                i++;
            } while (i < n);
            double  S_n = 0;
            for (i = 0; i < n; i++)
                S_n = E[i] + S_n;
            double t = S_n / n;
            if (t == 0 || t == 1)
                return 0;
            else if (Math.Abs(t - 0.5) >= (2 / Math.Sqrt(n)))
                return 0;
            else
                for (i = 0; i < n - 1; i++)
                {
                    if (E[i] == E[i + 1])
                        r_i = 0;
                    else
                        r_i = 1;
                    V_obs = r_i + V_obs;
                }
            V_obs = V_obs + 1;
            return cephes_erfc(Math.Abs(V_obs - 2 * n * t * (1 - t)) / (Math.Sqrt(2 * n)) * 2 * t * (1 - t));

        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {


        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            label5.Text = "";
            label6.Text = "";
            label8.Text = "";

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {            
           string str = Convert.ToString(textBox1.Text);
           label5.Text = str.Length.ToString();
            double P_value_frequency = Caculater_frequency(str);
            double P_value_runs = Caculater_runs(str);
            if (P_value_frequency < 0.01) 
                label6.Text = "P_value=" + P_value_frequency + "   失敗";
            else 
                label6.Text = "P_value=" + P_value_frequency + "   成功";
            if (P_value_runs < 0.01)
                label8.Text = "P_value=" + P_value_runs + "   失敗";
            else
                label8.Text = "P_value=" + P_value_runs + "   成功";
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void label5_Click(object sender, EventArgs e)
        {
            


        }
    }
}

發佈了32 篇原創文章 · 獲贊 4 · 訪問量 3091
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章