玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

隨機數種子產生方法

Posted on 2008-03-08 08:30 玄铁剑 阅读(541) 评论(0)  编辑 收藏 引用 所属分类: asp.netC# Winform

        static public int GetSeed()
        {
            int iSeed = 0;
            byte[] idArray = Guid.NewGuid().ToByteArray();
            int id1, id2, id3, id4;
            id1 = id2 = id3 = id4 = 0;
            id1 |= (int)idArray[0];
            id1 |= (int)idArray[1] << 8;
            id1 |= (int)idArray[2] << 16;
            id1 |= (int)idArray[3] << 24;
            id2 |= (int)idArray[4];
            id2 |= (int)idArray[5] << 8;
            id2 |= (int)idArray[6] << 16;
            id2 |= (int)idArray[7] << 24;
            id3 |= (int)idArray[8];
            id3 |= (int)idArray[9] << 8;
            id3 |= (int)idArray[10] << 16;
            id3 |= (int)idArray[11] << 24;
            id4 |= (int)idArray[12];
            id4 |= (int)idArray[13] << 8;
            id4 |= (int)idArray[14] << 16;
            id4 |= (int)idArray[15] << 24;
            iSeed = id1 ^ id2 ^ id3 ^ id4;
            return iSeed;
        }

static int GetRandomSeed( )
{
        
byte[] bytes = new byte[4];
        System.Security.Cryptography.RNGCryptoServiceProvider rng 
= new System.Security.Cryptography.RNGCryptoServiceProvider( );
        rng.GetBytes( bytes );
        
return BitConverter.ToInt32( bytes , 0 );
}

只有注册用户登录后才能发表评论。