-3

每当我的应用选择“随机”牌时,它总是遵循相同的奇怪模式:只有梅花 1-3,方块 4-6,红心 7-9,黑桃 10-12。西装总是在那个数字范围内,永远不会改变。每个套房应该被允许去13!请帮我解决一些令人困惑的问题,谢谢!

来源:

    Random Suite = new Random();
    Random Value = new Random();
    int sprod;
    int vprod;

     public Card RandomCardDraw()
     {
        sprod = Suite.Next(1, 5);

        Card newc = new Card();

        newc.x = sprod;

        vprod = Value.Next(1, 13);

        if (sprod == 1)
        {
            newc.suite = CardSuites.Club;

            newc.y = vprod;

            switch (vprod)
            {
                case 1:
                    newc.cardval = CardValues.Ace;
                    return newc;
                case 2:
                    newc.cardval = CardValues.Two;
                    return newc;
                case 3:
                    newc.cardval = CardValues.Three;
                    return newc;
                case 4:
                    newc.cardval = CardValues.Four;
                    return newc;
                case 5:
                    newc.cardval = CardValues.Five;
                    return newc;
                case 6:
                    newc.cardval = CardValues.Six;
                    return newc;
                case 7:
                    newc.cardval = CardValues.Seven;
                    return newc;
                case 8:
                    newc.cardval = CardValues.Eight;
                    return newc;
                case 9:
                    newc.cardval = CardValues.Nine;
                    return newc;
                case 10:
                    newc.cardval = CardValues.Ten;
                    return newc;
                case 11:
                    newc.cardval = CardValues.Jack;
                    return newc;
                case 12:
                    newc.cardval = CardValues.Queen;
                    return newc;
                case 13:
                    newc.cardval = CardValues.King;
                    return newc;
            }
        }
        if (sprod == 2)
        {
            newc.suite = CardSuites.Diamond;

            newc.y = vprod;

            switch (vprod)
            {
                case 1:
                    newc.cardval = CardValues.Ace;
                    return newc;
                case 2:
                    newc.cardval = CardValues.Two;
                    return newc;
                case 3:
                    newc.cardval = CardValues.Three;
                    return newc;
                case 4:
                    newc.cardval = CardValues.Four;
                    return newc;
                case 5:
                    newc.cardval = CardValues.Five;
                    return newc;
                case 6:
                    newc.cardval = CardValues.Six;
                    return newc;
                case 7:
                    newc.cardval = CardValues.Seven;
                    return newc;
                case 8:
                    newc.cardval = CardValues.Eight;
                    return newc;
                case 9:
                    newc.cardval = CardValues.Nine;
                    return newc;
                case 10:
                    newc.cardval = CardValues.Ten;
                    return newc;
                case 11:
                    newc.cardval = CardValues.Jack;
                    return newc;
                case 12:
                    newc.cardval = CardValues.Queen;
                    return newc;
                case 13:
                    newc.cardval = CardValues.King;
                    return newc;
            }
        }
        if (sprod == 3)
        {
            newc.suite = CardSuites.Heart;

            newc.y = vprod;

            switch (vprod)
            {
                case 1:
                    newc.cardval = CardValues.Ace;
                    return newc;
                case 2:
                    newc.cardval = CardValues.Two;
                    return newc;
                case 3:
                    newc.cardval = CardValues.Three;
                    return newc;
                case 4:
                    newc.cardval = CardValues.Four;
                    return newc;
                case 5:
                    newc.cardval = CardValues.Five;
                    return newc;
                case 6:
                    newc.cardval = CardValues.Six;
                    return newc;
                case 7:
                    newc.cardval = CardValues.Seven;
                    return newc;
                case 8:
                    newc.cardval = CardValues.Eight;
                    return newc;
                case 9:
                    newc.cardval = CardValues.Nine;
                    return newc;
                case 10:
                    newc.cardval = CardValues.Ten;
                    return newc;
                case 11:
                    newc.cardval = CardValues.Jack;
                    return newc;
                case 12:
                    newc.cardval = CardValues.Queen;
                    return newc;
                case 13:
                    newc.cardval = CardValues.King;
                    return newc;
            }
        }
        if (sprod == 4)
        {
            newc.suite = CardSuites.Spade;

            newc.y = vprod;

            switch (vprod)
            {
                case 1:
                    newc.cardval = CardValues.Ace;
                    return newc;
                case 2:
                    newc.cardval = CardValues.Two;
                    return newc;
                case 3:
                    newc.cardval = CardValues.Three;
                    return newc;
                case 4:
                    newc.cardval = CardValues.Four;
                    return newc;
                case 5:
                    newc.cardval = CardValues.Five;
                    return newc;
                case 6:
                    newc.cardval = CardValues.Six;
                    return newc;
                case 7:
                    newc.cardval = CardValues.Seven;
                    return newc;
                case 8:
                    newc.cardval = CardValues.Eight;
                    return newc;
                case 9:
                    newc.cardval = CardValues.Nine;
                    return newc;
                case 10:
                    newc.cardval = CardValues.Ten;
                    return newc;
                case 11:
                    newc.cardval = CardValues.Jack;
                    return newc;
                case 12:
                    newc.cardval = CardValues.Queen;
                    return newc;
                case 13:
                    newc.cardval = CardValues.King;
                    return newc;
            }
        }
        else
        {
            return newc;
        }
        return newc;
    }
4

2 回答 2

1

您在许多人遇到的问题上遇到了细微的变化Random。默认构造函数使用基于Environment.TickCount. 如果您快速创建两个实例,它们最终会得到相同的种子,因为系统计时器尚未更新。因此,两个实例都产生相同的数字序列。对受保护方法Next(int,int)生成的双精度进行一些简单的缩放。Sample只需使用单个随机实例并调用Next(1,5)or Next(1,13)

于 2013-08-04T01:33:18.317 回答
0

如果CardValuesCardSuites是枚举,则所有代码都可以有效地替换为

static Random random = new Random();

static Card RandomCardDraw()
{
    var suite = random.Next(5);
    var value = random.Next(14);
    return Card
    {
            x = suite + 1,
            y = value + 1,
            suite   = (CardSuites)suite,
            cardval = (CardValues)value,
    };
}

笔记:

您只需要一个Random实例。同时创建的两个实例将具有相同的种子并产生相关序列(或相同的序列,如果提供相同的参数)。

的最后一个参数Random.Next()排他性的。

于 2013-08-04T01:33:34.007 回答