samedi 21 mai 2022

How to limit random binary generation with a for-loop? (More details in question, including code)

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);
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire