当前位置: 代码迷 >> ASP.NET >> 获取随机数,该怎么处理
  详细解决方案

获取随机数,该怎么处理

热度:5305   发布时间:2013-02-25 00:00:00.0
获取随机数
。net 获取 1----100000 随机数分散不开 怎么办?

static Random rs;
  protected void Page_Load(object sender, EventArgs e)
  {
  if (rs == null)
  {
  rs = new Random();
  }
  int aaa = (int)(rs.NextDouble() * 100000);
  }

------解决方案--------------------------------------------------------
try

C# code
        private static int randomCount = 0;        private static string CreateRandomText()        {            randomCount++;            Guid guid = Guid.NewGuid( );            int key1 = guid.GetHashCode( );            int key2 = unchecked( (int)DateTime.Now.Ticks );            int seed = unchecked( key1 * key2 * randomCount );            Random rand = new Random( seed );            int n = rand.Next( 100000, 999999 );            string sn = n.ToString( );            for ( int j = 0 ; j < 6 ; j++ )            {                char ch = Convert.ToChar( rand.Next( 65, 90 ) );                if ( ch == 'E' || ch == 'O' || ch == 'S' || ch == 'B' || ch == 'I'                     || ch == 'L' || ch == 'D' || ch == 'C' || ch == 'G' )                {                    break;                }                sn = sn.Replace( rand.Next( 1, 9 ).ToString( ), ch.ToString( ) );            }            sn = sn.Replace( "0", rand.Next( 2, 9 ).ToString( ) );            sn = sn.Replace( "1", rand.Next( 2, 9 ).ToString( ) );            sn = sn.Replace( "5", rand.Next( 6, 9 ).ToString( ) );            return sn;        }
------解决方案--------------------------------------------------------
public int[] GetRandomArray(int Number,int minNum,int maxNum)
{
int j;
int[] b=new int[Number];
Random r=new Random();
for(j=0;j<Number;j++)
{
int i=r.Next(minNum,maxNum+1);
int num=0;
for(int k=0;k<j;k++)
{
if(b[k]==i)
{
num=num+1;
}
}
if(num==0 )
{
b[j]=i;
}
else
{
j=j-1;
}
}
return b;
}

------解决方案--------------------------------------------------------
Random random = new Random(DateTime.Now.Millisecond); 


或者


protected void Page_Load(object sender, EventArgs e) 


Random rs = new Random(); 

int aaa = rs.Next(1,100001); 
}
------解决方案--------------------------------------------------------
int codelength = 6;


string NumStr = null;
int RadNum = r.Next(999999);
if (RadNum.ToString().Length != 6)
{

for (int j = 1; j <= 6 - RadNum.ToString().Length; j++)
{
NumStr += "0";
}
NumStr += RadNum.ToString();
}
else
{
NumStr = RadNum.ToString();
}
string RandomCode = NumStr;

这个应该可以
  相关解决方案