CryptographicBuffer.GenerateRandom(UInt32) 方法

定义

创建包含随机数据的缓冲区。

public:
 static IBuffer ^ GenerateRandom(unsigned int length);
 static IBuffer GenerateRandom(uint32_t const& length);
public static IBuffer GenerateRandom(uint length);
function generateRandom(length)
Public Shared Function GenerateRandom (length As UInteger) As IBuffer

参数

length
UInt32

unsigned int

uint32_t

要创建的缓冲区的长度(以字节为单位)。

返回

包含随机数据的输出缓冲区。

示例


using Windows.Security.Cryptography;
using Windows.Storage.Streams;

namespace Random
{
    sealed partial class GenerateRandomDataApp : Application
    {
        public GenerateRandomDataApp()
        {
            // Initialize the application.
            this.InitializeComponent();

            // Create a buffer that contains random data.
            String strRndHex = this.GenerateRndData();

            // Create a random integer.
            UInt32 uRnd = this.GenerateRndNumber();
        }

        public String GenerateRndData()
        {
            // Define the length, in bytes, of the buffer.
            UInt32 length = 32;

            // Generate random data and copy it to a buffer.
            IBuffer buffer = CryptographicBuffer.GenerateRandom(length);

            // Encode the buffer to a hexadecimal string (for display).
            String hexRnd = CryptographicBuffer.EncodeToHexString(buffer);

            return hexRnd;
        }

        public UInt32 GenerateRndNumber()
        {
            // Generate a random number.
            UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();

            return Rnd;
        }
    }
}

适用于