在类库横行的今天请支持DIY 直接看代码(有注释)
view plaincopy to clipboardprint?
using System;
namespace MyRandom
{
public class Rand
{
private long seed; //随机数种子
//用系统时间作为随机种子
public Rand()
{
string str = DateTimeNowDayToString();
str += DateTimeNowHourToString();
str += DateTimeNowMinuteToString();
str += DateTimeNowSecondToString();
str += DateTimeNowMillisecondToString();
thisseed = longParse(str);
}
//用户自定义随机种子
public Rand(long Value)
{
thisseed = Value;
}
//产生非负伪随机数
public long Next()
{
long l = thisseed;
l = l * l;
string strTime = lToString();
int w = strTimeLength / ;
string str = ;
for (int i = w; i < strTimeLength w; i++)
str += strTime[i];
l = longParse(str);
return l;
}
//产生上限为Value的伪随机数
public long Next(long Value)
{
if (Value < )
return ;
long l = thisseed;
l = l * l;
string strTime = lToString();
int w = strTimeLength / ;
string str = ;
for (int i = w; i < strTimeLength w; i++)
str += strTime[i];
l = longParse(str);
return l % Value;
}
//产生下限为minValue上限为maxValue的伪随机数
public long Next(long minValue long maxValue)
{
if (minValue < || maxValue < || minValue > maxValue)
return ;
long l = thisseed;
l = l * l;
string strTime = lToString();
int w = strTimeLength / ;
string str = ;
for (int i = w; i < strTimeLength w; i++)
str += strTime[i];
l = longParse(str);
return l % (maxValue minValue) + minValue;
}
//伪随机数填充指定的比特数组
public void NextBytes(byte[] buffer)
{
long Value = ;
Value = thisNext() % ;
for (int i = ; i < bufferLength; i++)
{
Value = Value * Value;
string strTime = ValueToString();
int w = strTimeLength / ;
string str = ;
for (int j = w; j < strTimeLength w; j++)
str += strTime[j];
Value = longParse(str);
Value = Value % ;
buffer[i] = (byte)Value;
}
}
//产生到的伪随机数
public double NextDouble()
{
long l = thisNext( );
if (l == )
return ;
l = l * l;
string strTime = lToString();
int w = strTimeLength / ;
string str = ;
for (int i = w; i < strTimeLength w; i++)
str += strTime[i];
double d = doubleParse(str);
return d;
}
}
}