C# 为多个对象提供相同名称的随机名称生成器

C# 为多个对象提供相同名称的随机名称生成器,c#,string,random,C#,String,Random,我正在做一个爱好项目,创建一个“世界生成器”。我试图用许多“人类”对象填充我的世界,每个对象都有一个随机生成的名称。我有一个类名生成器,它有一个函数generateName,每次调用它时都会生成一个随机名称。在我的世界里,我每秒运行一个“day”函数,将100人添加到人员列表中。由于某些原因,在运行此操作时,许多人对象会生成相同的名称,尽管它们应该有不同的名称 这是我的密码: mainGui.cs using System; using System.Collections.Generic;

我正在做一个爱好项目,创建一个“世界生成器”。我试图用许多“人类”对象填充我的世界,每个对象都有一个随机生成的名称。我有一个类名生成器,它有一个函数generateName,每次调用它时都会生成一个随机名称。在我的世界里,我每秒运行一个“day”函数,将100人添加到人员列表中。由于某些原因,在运行此操作时,许多人对象会生成相同的名称,尽管它们应该有不同的名称


这是我的密码:

mainGui.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    public partial class WorldGeneratorForm : Form
    {
        World world = null;

        public WorldGeneratorForm()
        {
            InitializeComponent();
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            if (runButton.Text == "Run")
            {
                if (world == null) world = new World(this, 1000);
                world.run();

                runButton.Text = "Stop";
                statusLabel.Text = "Status: Active";
            }
            else if (runButton.Text == "Stop")
            {
                world.pause();

                runButton.Text = "Run";
                statusLabel.Text = "Status: Inactive";
            }
        }

        public void updateTimeElapsed(int i)
        {
            this.daysElapsedLabel.Text = "Days Elapsed: " + i;
        }

        public void updatePersonList(List<Human> list)
        {
            foreach (Human h in list) this.peopleList.Items.Add(h);
            peopleList.DisplayMember = "FullName";
        }
    }
}
namespace JamiesWorldGenerator
{
    partial class WorldGeneratorForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.populationLabel = new System.Windows.Forms.Label();
            this.runButton = new System.Windows.Forms.Button();
            this.statusLabel = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.countriesList = new System.Windows.Forms.ListBox();
            this.panel2 = new System.Windows.Forms.Panel();
            this.peopleList = new System.Windows.Forms.ListBox();
            this.panel3 = new System.Windows.Forms.Panel();
            this.worldNews = new System.Windows.Forms.ListBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.daysElapsedLabel = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // populationLabel
            // 
            this.populationLabel.AutoSize = true;
            this.populationLabel.Location = new System.Drawing.Point(9, 9);
            this.populationLabel.Name = "populationLabel";
            this.populationLabel.Size = new System.Drawing.Size(94, 13);
            this.populationLabel.TabIndex = 0;
            this.populationLabel.Text = "World Population: ";
            // 
            // runButton
            // 
            this.runButton.Location = new System.Drawing.Point(8, 320);
            this.runButton.Name = "runButton";
            this.runButton.Size = new System.Drawing.Size(75, 23);
            this.runButton.TabIndex = 1;
            this.runButton.Text = "Run";
            this.runButton.UseVisualStyleBackColor = true;
            this.runButton.Click += new System.EventHandler(this.runButton_Click);
            // 
            // statusLabel
            // 
            this.statusLabel.AutoSize = true;
            this.statusLabel.Location = new System.Drawing.Point(89, 325);
            this.statusLabel.Name = "statusLabel";
            this.statusLabel.Size = new System.Drawing.Size(81, 13);
            this.statusLabel.TabIndex = 2;
            this.statusLabel.Text = "Status: Inactive";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.countriesList);
            this.panel1.Location = new System.Drawing.Point(12, 25);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(197, 187);
            this.panel1.TabIndex = 3;
            // 
            // countriesList
            // 
            this.countriesList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.countriesList.FormattingEnabled = true;
            this.countriesList.Location = new System.Drawing.Point(0, 0);
            this.countriesList.Name = "countriesList";
            this.countriesList.Size = new System.Drawing.Size(197, 187);
            this.countriesList.TabIndex = 1;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.peopleList);
            this.panel2.Location = new System.Drawing.Point(215, 25);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(197, 187);
            this.panel2.TabIndex = 4;
            // 
            // peopleList
            // 
            this.peopleList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.peopleList.FormattingEnabled = true;
            this.peopleList.Location = new System.Drawing.Point(0, 0);
            this.peopleList.Name = "peopleList";
            this.peopleList.Size = new System.Drawing.Size(197, 187);
            this.peopleList.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.worldNews);
            this.panel3.Location = new System.Drawing.Point(12, 219);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(400, 95);
            this.panel3.TabIndex = 5;
            // 
            // worldNews
            // 
            this.worldNews.Dock = System.Windows.Forms.DockStyle.Fill;
            this.worldNews.FormattingEnabled = true;
            this.worldNews.Location = new System.Drawing.Point(0, 0);
            this.worldNews.Name = "worldNews";
            this.worldNews.Size = new System.Drawing.Size(400, 95);
            this.worldNews.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(336, 320);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "Load World";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(255, 320);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 7;
            this.button2.Text = "Save World";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // daysElapsedLabel
            // 
            this.daysElapsedLabel.AutoSize = true;
            this.daysElapsedLabel.Location = new System.Drawing.Point(212, 9);
            this.daysElapsedLabel.Name = "daysElapsedLabel";
            this.daysElapsedLabel.Size = new System.Drawing.Size(84, 13);
            this.daysElapsedLabel.TabIndex = 8;
            this.daysElapsedLabel.Text = "Days Elapsed: 0";
            // 
            // WorldGeneratorForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(423, 351);
            this.Controls.Add(this.daysElapsedLabel);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.statusLabel);
            this.Controls.Add(this.runButton);
            this.Controls.Add(this.populationLabel);
            this.Name = "WorldGeneratorForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Jamie\'s World Generator";
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel3.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label populationLabel;
        private System.Windows.Forms.Button runButton;
        private System.Windows.Forms.Label statusLabel;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.ListBox countriesList;
        private System.Windows.Forms.ListBox peopleList;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.ListBox worldNews;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label daysElapsedLabel;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    public class World
    {
        private delegate void updateForm();

        WorldGeneratorForm gui = null;

        List<Country> worldCountries = new List<Country>();
        List<Human> worldHumans = new List<Human>();

        List<Human> newWorldHumans;

        System.Timers.Timer timer = new System.Timers.Timer();
        int daysElapsed = 0;

        public World(WorldGeneratorForm mainGui, int dayDelay)
        {
            gui = mainGui;

            timer.Interval = dayDelay;
            timer.Elapsed += day;
        }

        private void day(object sender, EventArgs e)
        {
            newWorldHumans = new List<Human>();

            for (int i = 0; i < 100; i++)
            {
                NameGenerator nameGen = new NameGenerator();
                Human h = new Human(nameGen.generateName(4, 8), nameGen.generateName(4, 12));
                worldHumans.Add(h);
                newWorldHumans.Add(h);

            }

            daysElapsed++;
            Delegate updateDays = new updateForm(updateDayElapsed);
            Delegate updatePersonList = new updateForm(updateWorldPopulationList);
            gui.Invoke(updateDays);
            gui.Invoke(updatePersonList);
        }

        public void run()
        {
            timer.Enabled = true;
        }

        public void pause()
        {
            timer.Enabled = false;
        }

        private void updateDayElapsed()
        {
            gui.updateTimeElapsed(daysElapsed);
        }

        private void updateWorldPopulationList()
        {
            gui.updatePersonList(newWorldHumans);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace JamiesWorldGenerator
{
    public class Human
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }

        public Human(string fname, string lname)
        {
            FirstName = capitaliseFirst(fname);
            LastName = capitaliseFirst(lname);
            FullName = FirstName + " " + LastName;
        }

        string capitaliseFirst(string s)
        {
            if (s.Length == 0) return "";
            char[] chars = s.ToCharArray();
            chars[0] = Char.ToUpperInvariant(chars[0]);
            return new string(chars);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new WorldGeneratorForm());
        }
    }
}
Program.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    public partial class WorldGeneratorForm : Form
    {
        World world = null;

        public WorldGeneratorForm()
        {
            InitializeComponent();
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            if (runButton.Text == "Run")
            {
                if (world == null) world = new World(this, 1000);
                world.run();

                runButton.Text = "Stop";
                statusLabel.Text = "Status: Active";
            }
            else if (runButton.Text == "Stop")
            {
                world.pause();

                runButton.Text = "Run";
                statusLabel.Text = "Status: Inactive";
            }
        }

        public void updateTimeElapsed(int i)
        {
            this.daysElapsedLabel.Text = "Days Elapsed: " + i;
        }

        public void updatePersonList(List<Human> list)
        {
            foreach (Human h in list) this.peopleList.Items.Add(h);
            peopleList.DisplayMember = "FullName";
        }
    }
}
namespace JamiesWorldGenerator
{
    partial class WorldGeneratorForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.populationLabel = new System.Windows.Forms.Label();
            this.runButton = new System.Windows.Forms.Button();
            this.statusLabel = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.countriesList = new System.Windows.Forms.ListBox();
            this.panel2 = new System.Windows.Forms.Panel();
            this.peopleList = new System.Windows.Forms.ListBox();
            this.panel3 = new System.Windows.Forms.Panel();
            this.worldNews = new System.Windows.Forms.ListBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.daysElapsedLabel = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // populationLabel
            // 
            this.populationLabel.AutoSize = true;
            this.populationLabel.Location = new System.Drawing.Point(9, 9);
            this.populationLabel.Name = "populationLabel";
            this.populationLabel.Size = new System.Drawing.Size(94, 13);
            this.populationLabel.TabIndex = 0;
            this.populationLabel.Text = "World Population: ";
            // 
            // runButton
            // 
            this.runButton.Location = new System.Drawing.Point(8, 320);
            this.runButton.Name = "runButton";
            this.runButton.Size = new System.Drawing.Size(75, 23);
            this.runButton.TabIndex = 1;
            this.runButton.Text = "Run";
            this.runButton.UseVisualStyleBackColor = true;
            this.runButton.Click += new System.EventHandler(this.runButton_Click);
            // 
            // statusLabel
            // 
            this.statusLabel.AutoSize = true;
            this.statusLabel.Location = new System.Drawing.Point(89, 325);
            this.statusLabel.Name = "statusLabel";
            this.statusLabel.Size = new System.Drawing.Size(81, 13);
            this.statusLabel.TabIndex = 2;
            this.statusLabel.Text = "Status: Inactive";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.countriesList);
            this.panel1.Location = new System.Drawing.Point(12, 25);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(197, 187);
            this.panel1.TabIndex = 3;
            // 
            // countriesList
            // 
            this.countriesList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.countriesList.FormattingEnabled = true;
            this.countriesList.Location = new System.Drawing.Point(0, 0);
            this.countriesList.Name = "countriesList";
            this.countriesList.Size = new System.Drawing.Size(197, 187);
            this.countriesList.TabIndex = 1;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.peopleList);
            this.panel2.Location = new System.Drawing.Point(215, 25);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(197, 187);
            this.panel2.TabIndex = 4;
            // 
            // peopleList
            // 
            this.peopleList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.peopleList.FormattingEnabled = true;
            this.peopleList.Location = new System.Drawing.Point(0, 0);
            this.peopleList.Name = "peopleList";
            this.peopleList.Size = new System.Drawing.Size(197, 187);
            this.peopleList.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.worldNews);
            this.panel3.Location = new System.Drawing.Point(12, 219);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(400, 95);
            this.panel3.TabIndex = 5;
            // 
            // worldNews
            // 
            this.worldNews.Dock = System.Windows.Forms.DockStyle.Fill;
            this.worldNews.FormattingEnabled = true;
            this.worldNews.Location = new System.Drawing.Point(0, 0);
            this.worldNews.Name = "worldNews";
            this.worldNews.Size = new System.Drawing.Size(400, 95);
            this.worldNews.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(336, 320);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "Load World";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(255, 320);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 7;
            this.button2.Text = "Save World";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // daysElapsedLabel
            // 
            this.daysElapsedLabel.AutoSize = true;
            this.daysElapsedLabel.Location = new System.Drawing.Point(212, 9);
            this.daysElapsedLabel.Name = "daysElapsedLabel";
            this.daysElapsedLabel.Size = new System.Drawing.Size(84, 13);
            this.daysElapsedLabel.TabIndex = 8;
            this.daysElapsedLabel.Text = "Days Elapsed: 0";
            // 
            // WorldGeneratorForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(423, 351);
            this.Controls.Add(this.daysElapsedLabel);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.statusLabel);
            this.Controls.Add(this.runButton);
            this.Controls.Add(this.populationLabel);
            this.Name = "WorldGeneratorForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Jamie\'s World Generator";
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel3.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label populationLabel;
        private System.Windows.Forms.Button runButton;
        private System.Windows.Forms.Label statusLabel;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.ListBox countriesList;
        private System.Windows.Forms.ListBox peopleList;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.ListBox worldNews;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label daysElapsedLabel;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    public class World
    {
        private delegate void updateForm();

        WorldGeneratorForm gui = null;

        List<Country> worldCountries = new List<Country>();
        List<Human> worldHumans = new List<Human>();

        List<Human> newWorldHumans;

        System.Timers.Timer timer = new System.Timers.Timer();
        int daysElapsed = 0;

        public World(WorldGeneratorForm mainGui, int dayDelay)
        {
            gui = mainGui;

            timer.Interval = dayDelay;
            timer.Elapsed += day;
        }

        private void day(object sender, EventArgs e)
        {
            newWorldHumans = new List<Human>();

            for (int i = 0; i < 100; i++)
            {
                NameGenerator nameGen = new NameGenerator();
                Human h = new Human(nameGen.generateName(4, 8), nameGen.generateName(4, 12));
                worldHumans.Add(h);
                newWorldHumans.Add(h);

            }

            daysElapsed++;
            Delegate updateDays = new updateForm(updateDayElapsed);
            Delegate updatePersonList = new updateForm(updateWorldPopulationList);
            gui.Invoke(updateDays);
            gui.Invoke(updatePersonList);
        }

        public void run()
        {
            timer.Enabled = true;
        }

        public void pause()
        {
            timer.Enabled = false;
        }

        private void updateDayElapsed()
        {
            gui.updateTimeElapsed(daysElapsed);
        }

        private void updateWorldPopulationList()
        {
            gui.updatePersonList(newWorldHumans);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace JamiesWorldGenerator
{
    public class Human
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }

        public Human(string fname, string lname)
        {
            FirstName = capitaliseFirst(fname);
            LastName = capitaliseFirst(lname);
            FullName = FirstName + " " + LastName;
        }

        string capitaliseFirst(string s)
        {
            if (s.Length == 0) return "";
            char[] chars = s.ToCharArray();
            chars[0] = Char.ToUpperInvariant(chars[0]);
            return new string(chars);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new WorldGeneratorForm());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间JamiesWorldGenerator
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新WorldGeneratorForm());
}
}
}
编辑-NameGenerator.cs对不起,我以为我也发了(我真傻)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间JamiesWorldGenerator
{
类名生成器
{
随机=新随机();
公共字符串generateName(int-minLength,int-maxLength)
{
字符串chars=“abcdefghijklmnopqrstuvxyz”;
字符串辅音=“bcdfghjklmnpqrstvxzw”;
字符串元音=“aeiouy”;
int length=random.Next(minLength,maxLength);
字符串名称=”;
for(int i=0;i


如果有任何帮助,我将不胜感激

我将支持放置NameGenerator nameGen=new NameGenerator();在World outsite for循环的day函数中的语句将解决您的问题

NameGenerator nameGen = new NameGenerator();
for (int i = 0; i < 100; i++)
{
   Human h = new Human(nameGen.generateName(4, 8), nameGen.generateName(4, 12));
   worldHumans.Add(h);
   newWorldHumans.Add(h);
}
NameGenerator namegene=new NameGenerator();
对于(int i=0;i<100;i++)
{
人类h=新人类(nameGen.generateName(4,8),nameGen.generateName(4,12));
世界人类。添加(h);
新增(h);
}

使NameGenerator具有带有初始种子的singleton Random,因此每次实例化NameGenerator并生成名称时,它总是不同的

class NameGenerator
{
    private static readonly Random staticRandom = new Random();
    private Random random {get;set;}

    public NameGenerator()
    {
        random = staticRandom;
    }

...

我想问题是随机生成器的种子。几乎可以肯定的是,问题出在
NameGenerator
——这似乎是您选择不与我们共享的一段代码。请提供类
NameGenerator
,或者至少提供方法
generateName(int x,int y)
请阅读并创建一个。您显示的所有代码都不相关,并且缺少相关代码。最好将有问题的代码隔离到一个新项目中,然后发布,但不要在做了自己的研究之后发布。我猜你是在循环中使用
new Random()
。@CodeCaster但不是在做了自己的研究之后,我猜你的意思是但不是在做自己的研究之前是的,这一个有效。为什么把它放在循环之外会有不同?@jamiewarker-因为当你调用
new Random()
而不提供种子时,它是根据当前的日期时间值进行种子设定的-从现代CPU的角度来看,这需要很长时间才能改变。