Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking Random Numbers in c#

Tags:

c#

random

Hello everyone I got a problem selecting random numbers to shuffke a deck of cards. To shuffle randomly we tried to pick a random card in every loop, but it seems like if we ran out of one kind, we cannot pick a different kind randomly. It goes into infinite loop. Any suggestions?

(Translation: 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 () {

}

}

like image 257
user3279394 Avatar asked Dec 22 '25 13:12

user3279394


1 Answers

Use a proper shuffle algorithm, such as 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();
}

Then:

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

As the link posted by @Paul does indicate - be aware that Random is not a particularly great random number generator. You can substitute a better one if you want, but the underlying shuffle algorithm remains the same.

like image 81
Matthew Watson Avatar answered Dec 24 '25 03:12

Matthew Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!