Random numbers in C#

前端 未结 6 689
我寻月下人不归
我寻月下人不归 2021-01-24 19:52

I get the error below when I try to run the application I am sure its something simple but I dont see it. What I am trying to do it when I click a button I have labeled Play. I

6条回答
  •  天命终不由人
    2021-01-24 20:40

    Setup one Random object and initialize it once.

    class Form1
    {
        ...
        Random rnd = new Random();
    }
    

    then to use it every time it is needed

    void RandomNumber(int min, int max)
    {
        int num = rnd.Next(min, max);
        ...
    }
    

    What happens everytime you call new() it re-seeds the random number and you may end up with the same numbers over and over. I have had this happend to me and it killed me

提交回复
热议问题