vendredi 13 janvier 2017

Get Random Number in C# for Unity

I need a Random Number. e.g 1-10, it will Generate 10 times but the same number never come Again. the scenario is this Script Loading newly Every Scene so, I can't doing that

here my code is :

  List<int> ran = new List<int>();
System.Random rnd = new System.Random();
public static int randomValue;

int tempRandom;
public int randomNum()
{
    if(ran.Count == 0)
    {
        ran.Add(0);
        ran.Add(1);
        ran.Add(2);
        ran.Add(3);
        ran.Add(4);
        ran.Add(5);
        ran.Add(6);
        ran.Add(7);
    }
    tempRandom = rnd.Next(0, ran.Count);
    randomValue = ran[randomValue];
    ran.RemoveAt(tempRandom);

    return randomValue;
}

}




Aucun commentaire:

Enregistrer un commentaire