在c#中选择随机数

时间:2014-02-06 12:25:04

标签: c# random

大家好,我在选择随机数码来解决一副牌时遇到了问题。为了随机洗牌我们试图在每个循环中选择一张随机卡,但似乎如果我们用完了一种,我们就不能随机选择其他类型。它进入无限循环。有什么建议?

(翻译:Maca = spade,sinek = club,kupa = heart,karo = diamond,vale = jack,kiz = queen,papaz = king)

    public class KartKarma : MonoBehaviour {
    public GameObject[] Deste;
    public static GameObject KupaAs, Kupa2, Kupa3, Kupa4, Kupa5, Kupa6, Kupa7, Kupa8, Kupa9, Kupa10, KupaVale, KupaKiz, KupaPapaz;
    public static GameObject KaroAs, Karo2, Karo3, Karo4, Karo5, Karo6, Karo7, Karo8, Karo9, Karo10, Karo11, Karo12, Karo13;
    public static GameObject SinekAs, Sinek2, Sinek3, Sinek4, Sinek5, Sinek6, Sinek7, Sinek8, Sinek9, Sinek10, Sinek11, Sinek12, Sinek13;
    public static GameObject MacaAs, Maca2, Maca3, Maca4, Maca5, Maca6, Maca7, Maca8, Maca9, Maca10, Maca11, Maca12, Maca13;
    public GameObject[] Kupa = {KupaAs, Kupa2, Kupa3, Kupa4, Kupa5, Kupa6, Kupa7, Kupa8, Kupa9, Kupa10, KupaVale, KupaKiz, KupaPapaz};
    public GameObject[] Karo = {KaroAs, Karo2, Karo3, Karo4, Karo5, Karo6, Karo7, Karo8, Karo9, Karo10, Karo11, Karo12, Karo13};
    public GameObject[] Sinek = {SinekAs, Sinek2, Sinek3, Sinek4, Sinek5, Sinek6, Sinek7, Sinek8, Sinek9, Sinek10, Sinek11, Sinek12, Sinek13};
    public GameObject[] Maca = {MacaAs, Maca2, Maca3, Maca4, Maca5, Maca6, Maca7, Maca8, Maca9, Maca10, Maca11, Maca12, Maca13};
    List<GameObject> deste = new List<GameObject>();

// Use this for initialization
void Start () {
    int i = 0;
    while(i < 26){
        int a = Random.Range(1,5);
        if (a == 1) {
            int b = Random.Range(0,13);
            if(Maca != null){
                while(Maca[b] == null){
                    b = Random.Range(0,13);
                }
                deste.Add(Maca[b]);
                Maca[b] = null;
            }
            else {
                while(a == 1)
                    a = Random.Range(2,5);
            }
        }

        if (a == 2) {
            int b = Random.Range(0,13);
            if(Sinek != null){
                while(Sinek[b] == null)
                    b = Random.Range(0,13);
                deste.Add (Sinek[b]);
                Sinek[b] = null;
            }
            else {
                while(a == 2)
                    a = Random.Range(1,5);
            }
        }

         if (a == 3) {
            int b = Random.Range(0,13);
            if(Karo != null){
                while(Karo[b] == null)
                    b = Random.Range(0,13);
                deste.Add (Karo[b]);
                Karo[b] = null;
            }
            else {
                while(a == 3)
                    a = Random.Range(1,5);
            }
        }

        if (a == 4) {
            int b = Random.Range(0,13);
            if(Kupa != null){
                while(Kupa[b] == null){
                    b = Random.Range(0,13);
                }
                deste.Add(Kupa[b]);
                Kupa[b] = null;
            }
            else {
                                    while(a == 4)
                    a = Random.Range(1,5);
            }
        }
        i++;
    }
}

void Update () {

}

}

3 个答案:

答案 0 :(得分:3)

使用适当的随机播放算法,例如Knuth's (aka Fisher-Yates)

/// <summary>Used to shuffle collections.</summary>

public class Shuffler
{
    /// <summary>Shuffles the specified array.</summary>
    /// <typeparam name="T">The type of the array elements.</typeparam>
    /// <param name="array">The array to shuffle.</param>

    public void Shuffle<T>(IList<T> array)
    {
        for (int n = array.Count; n > 1; )
        {
            int k = _rng.Next(n);
            --n;
            T temp = array[n];
            array[n] = array[k];
            array[k] = temp;
        }
    }

    private readonly Random _rng = new Random();
}

然后:

var shuffler = new Shuffler();
...
shuffler.Shuffle(deste);

由@Paul发布的链接确实表明 - 请注意Random不是一个特别好的随机数生成器。如果需要,您可以替换更好的一个,但底层的混洗算法保持不变。

答案 1 :(得分:2)

while(a == 1)
    a = Random.Range(2,5);

这是你无限循环的原因。另外,那部分:

while(Sinek[b] == null)
    b = Random.Range(0,13);

也很讨厌,因为它不能保证满足条件。

答案 2 :(得分:0)

我认为行

while(a == 1)
while(a == 2)
while(a == 3)
while(a == 4)

导致此问题。请重新检查你的逻辑。