Chinaunix首页 | 论坛 | 博客
  • 博客访问: 679262
  • 博文数量: 148
  • 博客积分: 4086
  • 博客等级: 上校
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-06 23:27
文章分类

全部博文(148)

文章存档

2013年(19)

2012年(9)

2011年(106)

2009年(14)

分类: WINDOWS

2011-09-18 17:16:39

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Collections;
using
System.Text.RegularExpressions;

namespace
random
{
   
class
Program
    {  

       
//求随机数平均值方法

        static double Ave(double[] a)
        {
          
double sum=0
;
          
foreach(double d in
a)
           {  
              
               sum
=sum+
d;
           }
          
double ave=sum/
a.Length;

          
return
ave;
        }
       
//求随机数方差方法

        static double Var(double[] v)
        {
       
//
    double tt = 2;
           
//double mm = tt ^ 2;


           
double sum1 = 0;
           
for (int i = 0; i < v.Length; i++
)
            {
               
double temp = v[i] *
v[i];
                sum1
= sum1 +
temp;
           
            }

           
double sum = 0
;
           
foreach (double d in
v)
            {
                sum
= sum +
d;
            }

           
double var = sum1 / v.Length - (sum / v.Length) * (sum /
v.Length);
           
return
var;
        }
       
       
//求正态分布的随机数

        static void Fenbu(double[] f)
        {  
           
//double fenbu=new double[f.Length ];

            for (int i = 0; i < f.Length; i++)
            {
               
double a = 0, b = 0
;
                a
=Math.Sqrt((-2)*
Math.Log(f[i], Math.E));
                b
= Math.Cos(2 * Math.PI *
f[i]);
                f[i]
= a * b * 0.3 + 1
;

            }

        }

       
static void Main(string
[] args)
        {  

           
//生成100个(0,1)之间的随机数

            Random ran = new Random();
           
double[] dou = new double[100
];
           
for(int i=0;i<dou.Length;i++
)
            {
               dou[i]
=
ran.NextDouble();
           
            }

           
//调用Ave方法、Var方法求得随机数均值和方差

            double avenum = Ave(dou);
           
double varnum =
Var(dou);

          
//
写入文件
          
//将100个随机数,均值,方差保存到文件“d:\C#练习\SourceData.txt”中

            string Datapath = (@"d:\C#练习\SourceData.txt");

             FileStream fs
= new
FileStream(Datapath, FileMode.Create);
             StreamWriter sw
= new
StreamWriter(fs);

            
for (int j = 0; j < dou.Length;j++
)
             {
                 sw.WriteLine(dou[j]);
               
             }
           
             sw.Write(
"100个随机数均值和方差分别是{0}和{1}"
,avenum,varnum);
             sw.Close();


            
//读取数据文件“d:\C#练习\SourceData.txt”

             FileStream fs1 = new FileStream(Datapath, FileMode.Open);
             StreamReader sr
= new
StreamReader(fs1);
            
string temp=null
;
            
string str = null
;
            
while((temp=sr.ReadLine())!=null
)
            {
                str
= str+temp+" "
;
            }
           
           
//对数组进行分割Regax

             Regex re = new Regex(" ");
            
string[] str1=
re.Split(str);
            
double[] nums=new double[str1.Length-2
];
            
            
for(int i=0;i<str1.Length-2;i++
)
             {
             
              nums[i]
=
Convert.ToDouble(str1[i]);
             }
       
           
//调用正态分布随机函数,求均值和方差

            Fenbu(nums);
           
double averesult=
Ave(nums);
           
double varresult =
Var(nums);

           
//
写入文件
           
//将100个随机数,均值,方差保存到文件“d:\C#练习\ResultData.txt”中

            string Resultpath = (@"d:\C#练习\ResultData.txt");

            FileStream fs2
= new
FileStream(Resultpath, FileMode.Create);
            StreamWriter sw1
= new
StreamWriter(fs2);

           
for (int j = 0; j < nums.Length; j++
)
            {
                sw1.WriteLine(nums[j]);

            }

            sw1.Write(
"100个随机数均值和方差分别是{0}和{1}"
, averesult, varresult);
            sw1.Close();

            Console.ReadKey();
   
        }
    }
}
阅读(5764) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~