lundi 20 juin 2016

C#: Generating different random numbers given same boundaries [duplicate]

This question already has an answer here:

I'm writing a program that randomly generates a quantified item list for a magic shop.

I have a function that is supposed to generate bounded random quantities depending on the rarity of an item.

public int SetItemQuantity(string Rarity)
        {


            if (Rarity == "Common")
            {
                Random rndQty = new Random();
                int commonQty = rndQty.Next(5, 10);
                return commonQty;
            }

            if (Rarity == "Uncommon")
            {
                Random rndQty = new Random();
                int uncommonQty = rndQty.Next(0, 5);
                return uncommonQty;
            }
            else
                return 0;

The problem is that when this function is later called in a loop and I have two items of the same rarity, they consistently return the same quantities.




Aucun commentaire:

Enregistrer un commentaire