vendredi 16 février 2018

Why below code gives me "tails" always? [duplicate]

This question already has an answer here:

Below code gives me "tails" always. I am trying to toss a coin and printout the result but I get tails everytime. I should seed random object with current time or should I change the code?

using System;

namespace Coin_Toss
{
class coin
{
    public string face;

    public coin()
    {
        face = "heads";
    }

    public void Toss()
    {
        Random rand = new Random();

        if (rand.Next(2) == 0)
        {
            face = "heads";
        }
        else face = "tails";
    }
}

class Program
{
    static void Main(string[] args)
    {
        coin c = new coin();

        for (int i = 0; i < 5; i++)
        {
            c.Toss();
            Console.WriteLine(c.face);
        }
    }
}
}




Aucun commentaire:

Enregistrer un commentaire