按一下以給予評分及指教
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework 類別庫
RNGCryptoServiceProvider 類別

使用由密碼編譯服務供應者 (CSP) 提供的實作 (implementation),實作密碼編譯亂數產生器 (RNG)。這個類別無法被繼承。

命名空間: System.Security.Cryptography
組件: mscorlib (在 mscorlib.dll 中)

Visual Basic (宣告)
<ComVisibleAttribute(True)> _
Public NotInheritable Class RNGCryptoServiceProvider
    Inherits RandomNumberGenerator
Visual Basic (使用方式)
Dim instance As RNGCryptoServiceProvider
C#
[ComVisibleAttribute(true)] 
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator
C++
[ComVisibleAttribute(true)] 
public ref class RNGCryptoServiceProvider sealed : public RandomNumberGenerator
J#
/** @attribute ComVisibleAttribute(true) */ 
public final class RNGCryptoServiceProvider extends RandomNumberGenerator
JScript
ComVisibleAttribute(true) 
public final class RNGCryptoServiceProvider extends RandomNumberGenerator

下列程式碼範例顯示如何使用 RNGCryptoServiceProvider 類別建立亂數。

Visual Basic
'The following sample uses the Cryptography class to simulate the roll of a dice.
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography



Class RNGCSP
    
    ' Main method.
    Public Shared Sub Main()
        ' Roll the dice 30 times and display 
        ' the results to the console.
        Dim x As Integer
        For x = 0 To 30
            Console.WriteLine(RollDice(6))
        Next x
    End Sub 'Main
     
    ' This method simulates a roll of the dice. The input parameter is the 
    ' number of sides of the dice.
    Public Shared Function RollDice(NumSides As Integer) As Integer
        ' Create a byte array to hold the random value.
        Dim randomNumber(0) As Byte
        
        ' Create a new instance of the RNGCryptoServiceProvider. 
        Dim Gen As 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.
        Dim rand As Integer = 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 Mod NumSides + 1
    End Function 'RollDice
End Class 'CryptoMemoryStream

C#
//The following sample uses the Cryptography class to simulate the roll of a dice.

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;
    }
}

C++
// The following sample uses the Cryptography class
// to simulate the roll of a dice.

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::Cryptography;

int RollDice(int numberSides)
{
    // Create a byte array to hold the random value.
    array<Byte>^ randomNumber = gcnew array<Byte>(1);

    // Create a new instance of the RNGCryptoServiceProvider.
    RNGCryptoServiceProvider^ cryptoProvider =
        gcnew RNGCryptoServiceProvider();

    // Fill the array with a random value.
    cryptoProvider->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 % numberSides) + 1;
}
    
int main()
{
    // Roll the dice 30 times and display
    // the results to the console.
    for (int i = 0; i <= 30; i++)
    {
        Console::WriteLine(RollDice(6));
    }
}

System.Object
   System.Security.Cryptography.RandomNumberGenerator
    System.Security.Cryptography.RNGCryptoServiceProvider
這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。

Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

.NET Framework

支援版本:2.0、1.1、1.0

.NET Compact Framework

支援版本:2.0
社群內容   什麼是社群內容?
新增內容      
Processing
© 2008 Microsoft Corporation. All rights reserved. 使用規定  |  商標  |  隱私權聲明
Page view tracker