Winforms 调试和运行时得到不同的结果(C#)

Winforms 调试和运行时得到不同的结果(C#),winforms,Winforms,所以,我用随机对象取一个随机数,来选择这个任务需要多少个UserControls(实际上是随机选择),在每个UserControl中,我再次使用随机对象,得到一个随机数,放在我的UC中。但由于某些原因,当我运行(Ctrl+F5)程序时,每个UserControl都包含相同的数字,例如,5个UC,其中的数字为1。 我在另一个线程中看到,您应该在循环(我创建用户控件的循环)之前声明变量(例如Random r),但我不能,因为我在UserControl的构造函数中使用Random 我让X用户控件的循环

所以,我用随机对象取一个随机数,来选择这个任务需要多少个UserControls(实际上是随机选择),在每个UserControl中,我再次使用随机对象,得到一个随机数,放在我的UC中。但由于某些原因,当我运行(Ctrl+F5)程序时,每个UserControl都包含相同的数字,例如,5个UC,其中的数字为1。 我在另一个线程中看到,您应该在循环(我创建用户控件的循环)之前声明变量(例如Random r),但我不能,因为我在UserControl的构造函数中使用Random

我让X用户控件的循环是:(num是我们从Random.Next()中得到的数字)
对于(int i=0;i用户控件中静态
Random
成员的快速示例:

public partial class ucSomething : UserControl
{

    private static Random R = new Random(); // only ONE instance of Random that gets reused across all ucSomething's...

    public ucSomething()
    {
        InitializeComponent();
        int number = R.Next(1, 11); // 1 to 10 inclusive
        for(int i = 1; i <= number; i++)
        {
            // ... something ...
        }
    }

}
公共部分类ucSomething:UserControl
{
private static Random R=new Random();//只有一个Random实例在所有ucSomething中被重用。。。
公共事务
{
初始化组件();
int number=R.Next(1,11);//1到10
对于(int i=1;我只创建Random类的一个实例。同时创建多个实例只会从每个实例中获得相同的随机数,因为它们都有相同的种子。将其公开并静态,以便您可以从任何地方访问它。