# .net 中随机数的产生

Using the Random Class

 In this article well be going over how to use the Random class found in the System Namespace. The Random class can be used to generate random numbers a number of different ways and in this article we will go though most of them. In the first part of the article well be discussing how to initialize a new instance of the Random class. Then in the second half of the article we will be going over 3 of its most useful methods: Next, NextBytes, and NextDouble. 在这篇文章中我们将讨论Random类的使用。Random类可以产生一个随机数，这篇文章我们将对这个进行研究。在第一部分，我们将论述如何初始化一个随机类的实列。然后，我们将论述它的三个重要的方法：Next, NextBytes, 和 NextDouble。
Initializing an Instance of the Random Class

Before we get into the different constructors for the Random class I want to discuss what a Seed is. The definition of a Seed is: A number used to calculate a starting value for a number sequence. What this means is, if you want to generate a random number that is not less than 50 then you want yours Seed to be 50.

The reason I wanted to quickly go over what a Seed was is because you will need to know what it is to understand how the constructor of the Random class determines what the seed is! The first overload for the Random class expects no parameters and uses a time dependent Seed value. The second overload expects an integer as a parameter. This integer value will be used as the Seed.

Code Example:

1. Random RA = new Random();
2. Random RA = new Random(15);
The numbers generated from the Random class are on random to a degree. If you use the same Seed value the same series of numbers will be returned. For instance, say you use 15 as the Seed value. You will receive this series of numbers over and over again for the first two values!

Code Example:

<script Language="C#" Runat="Server">

protected void Page_Load(Object sender, EventArgs e){

if (IsPostBack) {

Random ra = new Random(15);

int num = ra.Next();
VALUES.Text = VALUES.Text + "<BR>" + num.ToString();

num = ra.Next();
VALUES.Text = VALUES.Text + "<BR>" + num.ToString();
}
}
</script>

<HTML>
<BODY>
<FORM RUNAT="SERVER">
<ASP:LABEL RUNAT="SERVER" ID="VALUES" MaintainState="True"/>
<BR>
<ASP:Button RUNAT="SERVER" TEXT="SUBMIT" />
</FORM>
</BODY>
</HTML>
Random Numbers Generated:

1208223655 469449854
1208223655 469449854

As you can see you receive the same numbers for the first Random number and second Random number generated over and over again.

You can resolve this issue by using a method Microsoft recommends.Using the Systems Time as the seed value. Although, you can still run into problems here if the clocks speed on the computer which is very fast because the time may not change from one instance creation to another. Here is a great example found in the .NET SDK Help files to over come this problem.

Code Example:

<script Language="C#" Runat="Server">

protected void Page_Load(Object sender, EventArgs e){

Random Rnd1 = new Random(unchecked((int)DateTime.Now.Ticks));
Random Rnd1a = new Random(unchecked((int)DateTime.Now.Ticks));
Response.Write(Rnd1.Next().ToString());
Response.Write("<BR>");
Response.Write(Rnd1a.Next().ToString());

Response.Write("<BR>");

Random Rnd2 = new Random(~unchecked((int)DateTime.Now.Ticks));
Random Rnd2a = new Random(~unchecked((int)DateTime.Now.Ticks));
Response.Write(Rnd2.Next().ToString());
Response.Write("<BR>");
Response.Write(Rnd2a.Next().ToString());

}
</script>
You'll notice that Rnd1 and Rnd1a are identical, as with Rnd2 and Rnd2a - this is because the same algorithm is used to get their seed values. Using the bitwise operator ~ in Rnd2 & Rnd2a changes the resulting integers value, hence changing the seed value! For those who don't know using unchecked for an arithmetic equation trucates the result if it is out of the range of the resulting data type.

Next, NextBytes, and NextDouble Methods

Next, NextBytes, NextDouble方法

Now that we have seen how to construct the Random class, lets look at some of the methods we can use to generate random numbers. The first method I want to get into is the Next method. The Next method is used after the Random class has been initialized and is used to return the actual random number that is generated. You have seen me use this in some of the previous examples. There are three overloads for the Next method. The first takes no parameters, the second, takes one parameter that should be an integer. This value specifies the maximum value the returned random number should be. The third overload for the Next method expects two integer values. The first value designates the minimum value acceptable for the returned random number, and the second integer the maximum. So lets take a look at all three:

Code Example:

<script Language="C#" Runat="Server">

protected void Page_Load(Object sender, EventArgs e){

Random Rnd1 = new Random();

int num1 = Rnd1.Next();
int num2 = Rnd1.Next(1);
int num3 = Rnd1.Next(2, 3);

Response.Write(num1.ToString());
Response.Write("<BR>");
Response.Write(num2.ToString());
Response.Write("<BR>");
Response.Write(num3.ToString());

}

</script>
In this example num1 will be different nearly every time because its seed is generated by the system time. Num2 will always be 0 because 1 is always the max value. Finally, 2 will always be the value of num3 because the minvalue is 2 and the maxvalue is 3.

The next method I want to talk about is the NextBytes method. The NextBytes method expects one parameter, an array of bytes. The NextBytes method fills this array of bytes with Random numbers. The following example demonstrates how to use the NextBytes method:

Code Example:

<script Language="C#" Runat="Server">

protected void Page_Load(Object sender, EventArgs e){

Random Rnd1 = new Random();
byte[] bArray = new byte[10];

Rnd1.NextBytes(bArray);

for (int i = 0; i < bArray.Length; i ++){

Response.Write("<BR>");
Response.Write(bArray[i].ToString());

}

}

</script>
The final method I want to talk about is the NextDouble method. The NextDouble method has no parameters and returns a double precision point number <= .0 and > 1.0. An example of the NextDouble method can be found below:

Code Example:<script Language="C#" Runat="Server">

protected void Page_Load(Object sender, EventArgs e){

Random Rnd1 = new Random();
double dbl = Rnd1.NextDouble();
Response.Write(dbl.ToString());
Response.Write("<BR>");
dbl = Rnd1.NextDouble();
Response.Write(dbl.ToString());
}

</script>
Conclusion

As you can see by my examples the Random class is quite easy to use, but at the same time kind of tricky. Using the Random class to generate a random number by default doesn't mean you will receive a random number for every situation, but by using some very easy methods such as Seed values and different algorithms.

1、以上方法，beta 1 和 beta 2都适用。

2、本文出处：

http://www.aspnextgen.com/tutorials.aspx?tutorialid=106

• 本文已收录于以下专栏：

举报原因： 您举报文章：.net 中随机数的产生 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)