I am trying to make a simple C# console application that generates random binary code. The binary generation works fine, but I'm trying to limit it using a for-loop. However, this doesn't work. The application generates infinite binary code, so I don't know what I'm doing wrong. Please help! From my knowledge, the for-loop should only generate 5 random binary digits. Code:
using System;
using System.Collections.Generic;
public class BinaryGen
{
public static void Main()
{
Random rnd = new Random();
for (int i = 0; i < 5; i )
{
i = rnd.Next(0, 2);
Console.WriteLine(i);
}
}
}
CodePudding user response:
int b = 1;
for (int i = 1; i < 5; i )
{
Console.Write(b);
b = rnd.Next(0, 2);
}
You change the state of the counter i to 1 or 0, so the loop is infinite.