求随机数平均值方法 求随机数方差方法 求正态分布的随机数
2011-04-20 10:45
253 查看
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:\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:\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();
}
}
}
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:\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:\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();
}
}
}
相关文章推荐
- 有相同的平均值,产生不同方差的随机数
- 任意分布的随机数的产生方法—VC程序实现方法
- 生成特定分布随机数的方法
- 根据连续分布产生随机数一般方法
- 生成特定分布随机数的方法
- 任意分布的随机数的产生方法—VC程序实现方法
- 任意分布的随机数的产生方法
- Excel在统计分析中的应用—第二章—描述性统计-分组数据的算术平均值、方差及标准差求解方法
- C#利用Random得随机数求均值、方差、正态分布的方法
- 生成特定分布随机数的方法:Python seed() 函数&numpy &scikit-learn随机数据生成
- C++实现正态随机分布的方法
- 生成特定分布随机数的方法
- 任意分布的随机数的产生方法—VC++程序实现方法
- 生成均匀“随机数”,一种生成均匀分布数字的简单方法
- java生成随机数方法大全(可指定范围、分布方式、类型、种子)
- 【算法分析】随机数的生成:均匀分布、正态、泊松 、瑞利
- 任意分布的随机数的产生方法—VC程序实现方法
- OpenCV 自定义任意区域形状及计算平均值 方差
- 转载:JAVA生成给定范围内的不重复的随机数的几种方法
- 随机数,和,平均值