静态随机成员每次仍然输出相同的数字

时间:2015-08-20 01:34:35

标签: c# random

我有一个静态随机成员,当我使用rand.next(1,2)时,我总是得到1.我不明白为什么。

    static Random rand = new Random();

    private static void Attack(Player player, Monster monster)
    {
        var pDamage = Convert.ToInt32(Math.Floor(player.Weapon.Attack * monster.Armor.DamageRedux));
        Console.WriteLine("You strike the {0} for {1} damage.", monster.MonsterName, pDamage);
        monster.Health -= pDamage;
        for (int i = 0; i < 10; i++)
        {
            var hitRoll = rand.Next(1, 2);
            Console.Write("{0}", hitRoll);
        }
        switch (1)
        {
            case 1:
                var mDamage = Convert.ToInt32(Math.Floor(monster.Weapon.Attack * player.Armor.DamageRedux));
                Console.WriteLine("The {0} swing back and hits you for {1} damage.", monster.MonsterName, mDamage);
                player.Health -= mDamage;
                break;
            case 2:
                Console.WriteLine("The {0} swings wildly at you and misses.", monster.MonsterName);
                break;
        }
    }

2 个答案:

答案 0 :(得分:0)

当最小值包含时,最大值是独占的。目测:

minValue ●—— value ——○ maxValue

或作为表达

minValue <= value && value < maxValue

以下是Next的文档:https://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx

答案 1 :(得分:0)

您应该更改发送到Next()方法的值:

var hitRoll = rand.Next(1, 3); // Generates a value between 1 and 2