随机数自相关检验代码参考

项目参考

点击下载项目完整参考源代码

生成dll

组件中类的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary_test
{
    public class Class1
    {
        public int[] E = new int[200];
        public int n_len ;
        public double P_1,P_2,P_8,P_16;
        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 int A(int d)
        {
            int i;
            int a = 0;
            for (i = 0; i <= n_len-d-1; i++)
                a = (E[i]+E[i+d])%2 + a;
            return a;
        }
        public double V(int d)
        {
            double v = 0;
            v = 2 * (A(d)-((n_len-d)/2)) / Math.Sqrt(n_len - d);
            return v;
        }

        public void Caculater(string str)
        {
            n_len = str.Length;
            int i = 0;
            do
            {
                E[i] = Convert.ToInt32(str[i]) - 48;
                i++;
            } while (i < n_len);
           
            if (n_len >= 2) P_1 = cephes_erf(Math.Abs(V(1))/ Math.Sqrt(2));
            if (n_len >= 3) P_2 = cephes_erf(Math.Abs(V(2)) / Math.Sqrt(2));
            if (n_len >= 9) P_8 = cephes_erf(Math.Abs(V(8)) / Math.Sqrt(2));
            if (n_len >= 17) P_16 = cephes_erf(Math.Abs(V(16)) / Math.Sqrt(2));
        }

    }
}

wcf服务程序

IService1.cs中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        DateStruct RandomTest(string str);



        // TODO: 在此添加您的服务操作
    }


    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
    [DataContract]
    public class DateStruct
    {
      
        [DataMember]
        public double P1_value
        {
            get;
            set;
        }

        [DataMember]
        public double P2_value
        {
            get;
            set;
        }

        [DataMember]
        public double P8_value
        {
            get;
            set;
        }

        [DataMember]
        public double P16_value
        {
            get;
            set;
        }
        [DataMember]
        public string P1_result
        {
            get;
            set;
        }
        [DataMember]
        public string P2_result
        {
            get;
            set;
        }
        [DataMember]
        public string P8_result
        {
            get;
            set;
        }
        [DataMember]
        public string P16_result
        {
            get;
            set;
        }


    }
}

Service1.svc中的代码

using ClassLibrary_test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
    public class Service1 : IService1
    {
        public DateStruct RandomTest(string str)
        {
            DateStruct RandomTestresult = new DateStruct();
            Class1 myclass = new Class1();

            myclass.Caculater(str);
            RandomTestresult.P1_value = myclass.P_1;
            RandomTestresult.P2_value = myclass.P_2;
            RandomTestresult.P8_value = myclass.P_8;
            RandomTestresult.P16_value = myclass.P_16;
            if (RandomTestresult.P1_value >= 0.01)
                RandomTestresult.P1_result = "d=1时,自相关检验通过";
            else
                RandomTestresult.P1_result = "d=1时,自相关检验不通过";

            if (RandomTestresult.P2_value >= 0.01)
                RandomTestresult.P2_result = "d=2时,自相关检验通过";
            else
                RandomTestresult.P2_result = "d=2时,自相关检验不通过";

            if (RandomTestresult.P8_value >= 0.01)
                RandomTestresult.P8_result = "d=8时,自相关检验通过";
            else
                RandomTestresult.P8_result = "d=8时,自相关检验不通过";

            if (RandomTestresult.P16_value >= 0.01)
                RandomTestresult.P16_result = "d=16时,自相关检验通过";
            else
                RandomTestresult.P16_result = "d=16时,自相关检验不通过";

            return RandomTestresult;
        }

    }
}

mvc程序设计

控制器

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class TestController : Controller
    {
        private TestDataContext db = new TestDataContext();//TestOutput为模型类名称
        // GET: Test
        public ActionResult Index()
        {
            var lists = from t in db.Table_testoutput //表名称
                        orderby t.Id
                        descending    //倒序
                        select t;
            return View(lists.ToList());
        }

        // GET: Test/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Test/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Test/Create
        [HttpPost]
        public ActionResult Create(Table_testinput collection)
        {
            try
            {
                // TODO: Add insert logic here

                ServiceTest.Service1Client ww = new ServiceTest.Service1Client(); //定义一个调用服务的客户端
                ServiceTest.DateStruct ss = new ServiceTest.DateStruct();//声明一个引用服务的数据类的变量
                ss = ww.RandomTest(collection.Randoms); //执行调用服务的功能
                Table_testoutput n1 = new Table_testoutput();
                n1.Randoms = collection.Randoms;            //将输入值赋值于变量n1
                n1.P1_value = ss.P1_value;
                n1.P2_value = ss.P2_value;
                n1.P8_value = ss.P8_value;
                n1.P16_value = ss.P16_value;
                n1.P1_result = ss.P1_result;
                n1.P2_result = ss.P2_result;
                n1.P8_result = ss.P8_result;
                n1.P16_result = ss.P16_result;

                db.Table_testoutput.InsertOnSubmit(n1);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Test/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Test/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Test/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Test/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

两个表

CREATE TABLE [dbo].[Table_testinput] (
    [Id]      INT            IDENTITY (1, 1) NOT NULL,
    [Randoms] NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Table_testoutput] (
    [Id]         INT            IDENTITY (1, 1) NOT NULL,
    [Randoms]    NVARCHAR (MAX) NULL,
    [P1_value]   FLOAT (53)     NULL,
    [P2_value]   FLOAT (53)     NULL,
    [P8_value]   FLOAT (53)     NULL,
    [P16_value]  FLOAT (53)     NULL,
    [P1_result]  NVARCHAR (MAX) NULL,
    [P2_result]  NVARCHAR (MAX) NULL,
    [P8_result]  NVARCHAR (MAX) NULL,
    [P16_result] NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值