首页 > Web开发 > 详细

(转)使用 .NET 的 RNGCryptoServiceProvider 生成随机数

时间:2014-12-21 16:35:30      阅读:282      评论:0      收藏:0      [点我收藏+]

1. [代码]一个简单的方法,但不够可靠     跳至 [1] [2] [全屏预览]

1
2
3
4
5
6
7
8
9
10
11
static void Main(string[] args)
{
    // code from DevCurry.com
    byte[] randomBytes = new byte[4];
    Random rando = new Random();
    rando.NextBytes(randomBytes);
    foreach (byte byteValue in randomBytes)
        Console.Write("{0, 4}", byteValue);
 
    Console.ReadLine();
}

2. [代码]可靠的方法     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static void Main(string[] args)
{
    // code from DevCurry.com
    byte[] randomBytes = new byte[4];
    RNGCryptoServiceProvider rngCrypto =
    new RNGCryptoServiceProvider();
 
    rngCrypto.GetBytes(randomBytes);
    Int32 rngNum = BitConverter.ToInt32(randomBytes, 0);
 
    Console.WriteLine(rngNum);
 
    Console.ReadLine();
}

(转)使用 .NET 的 RNGCryptoServiceProvider 生成随机数

原文:http://www.cnblogs.com/wanshutao/p/4176691.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!