我写的这个课程是否足够(我是指专业人员这样做)被包含在代码/项目中?或者我错过了重要的事情?我不知道如何使用构造函数等,所以我没有使用相同的语言(我只是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”的各种方法。 –