Does "new Random (x)" always generate the same numbers?

I try to get a unique random number, but always get the same number every time I run the code. I will get 14 first and then 6, but my list for storing all used numbers does not seem to work. When using 14 manual jobs, but when I add randInt, it does not work.

const int numCards = 32;
        List<int> usedCards = new List<int>();
        int randInt = 0;
        Random rand = new Random(numCards);
        usedCards.Add(14);
        do
        {

            randInt = rand.Next(0, numCards);
            MessageBox.Show(randInt.ToString(), "End Game", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        while (usedCards.Contains(randInt));

        usedCards.Add(randInt);
+3
source share
2 answers

Replace:

Random rand = new Random(numCards);

with

Random rand = new Random();

Delivering a fixed initial value ( numCardsalways has the same value) in the call Randomwill result in a predictable, reproducible sequence that is always the same for the same initial value, exactly the same (not quite, but still the point is valid):

enter image description here

, 1 10 0 100,

24,11,46,77,65,43,35,94,10,64

, .

, , Random , - , , Random .

+17

Random, . .

, Random , , , , , .

Random, .

+3

All Articles