我寫的這個課程是否足夠(我是指專業人員這樣做)被包含在代碼/項目中?或者我錯過了重要的事情?我不知道如何使用構造函數等,所以我沒有使用相同的語言(我只是C#中的初學者),但如果需要,請發表評論。課堂設計質量
using System;
using System.Collections.Generic;
using System.Text;
namespace RandBit
{
/// <summary>
/// By: Author
/// Version 0.0.1
/// Pseudo-Random 16-Bit (Max) Generator.
/// </summary>
public class RandomBit
{
/// <param name="input">The Bit-size(int)</param>
/// <returns>Random Bit of Bit-size(string)</returns>
public static string Generate(int input)
{
int bitSize = 0;
Random choice = new Random();
if (input == 0 || input > 16)
{
bitSize = 0;
}
else if (input == 1)
{
bitSize = 1;
}
else
{
int randomChoice = choice.Next(0, (1 << input));
bitSize = randomChoice;
}
string binary = Convert.ToString(bitSize, 2);
binary = binary.PadLeft(input, '0');
return binary;
}
}
}
謝謝。
感謝您的意見。我如何解決這個問題? –
最重要的一點是保留一個RNG實例,而不是重複創建新的實例。我添加了一個示例,顯示了這樣做的一種方式,並且還使用了更強大的RNG。我鏈接的文章顯示了使用「隨機」和「RNGCryptoServiceProvider」的各種方法。 –