C#如何产生散列性比较好的随机数序列?时间用多一些都可以~~
解决方案 »
- WinForm上面不能正常显示控件
- 怎么使按钮按一下就改变一次颜色??
- 谁了解c#中封箱和拆箱的问题?
- asp.net(C#)中.表的修改问题.
- 请问各位Visual C++ Runtime Library Runtime Error怎么解决
- WinForm问题~~
- windows 控件开发 数据检索控件 消息事件传递 问题
- 奇怪的COOKIE问题,COOKIE怎么会不见了?抓包显示明明有的啊.
- C#如何保存picturebox上的图片,并且保存picturebox上的label控件
- 在线结贴,怎样限制float的小数点后的位数
- 把文件和文本一起提交到服务器(好比:邮箱一样,把附件和要传的邮件文本内容一起提交)是如何做的?
- 怎么用webclient登陆https的系统
命名空间:System.Security.CryptographyMSDN的例子:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;class RNGCSP
{
// Main method.
public static void Main()
{
// Roll the dice 30 times and display
// the results to the console.
for(int x = 0; x <= 30; x++)
Console.WriteLine(RollDice(6));
}
// This method simulates a roll of the dice. The input parameter is the
// number of sides of the dice.
public static int RollDice(int NumSides)
{
// Create a byte array to hold the random value.
byte[] randomNumber = new byte[1]; // Create a new instance of the RNGCryptoServiceProvider.
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider(); // Fill the array with a random value.
Gen.GetBytes(randomNumber); // Convert the byte to an integer value to make the modulus operation easier.
int rand = Convert.ToInt32(randomNumber[0]); // Return the random number mod the number
// of sides. The possible values are zero-
// based, so we add one.
return rand % NumSides + 1;
}
}
其中这种规律对于1到256都有效,但是数字RollDice(257)不能生成最大值257!
RollDice(250) * 250 + RollDice(250)