我有以下功能:
//Function to get random number
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它:
byte[] mac = new byte[6];
for (int x = 0; x < 6; ++x)
mac[x] = (byte)(Misc.RandomNumber((int)0xFFFF, (int)0xFFFFFF) % 256);
Run Code Online (Sandbox Code Playgroud)
如果我在运行时使用调试器执行该循环,则会得到不同的值(这就是我想要的).但是,如果我在该代码下面放置一个断点两行,则"mac"数组的所有成员都具有相同的值.
为什么会这样?
我在这里完全失败了...逻辑似乎设置正确但是while语句中的"响应"表示它在当前上下文中不存在.我在这里搜索,似乎在这种情况下似乎找到了相同的问题.问题是融合方法吗?
do
{
Console.WriteLine("enter a number between 1 and 5");
int x = Convert.ToInt32(Console.ReadLine());
Random r = new Random();
int rr = r.Next(1, 5);
Console.WriteLine("Do you want to continue? Please select yes or no.");
string response = Convert.ToString(Console.ReadLine());
} while (response == "yes");
Run Code Online (Sandbox Code Playgroud)