gpt4 book ai didi

c# - 在 C# 中实现蒙特卡洛模拟时的奇怪行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:34 28 4
gpt4 key购买 nike

我正在尝试使用带路径压缩的加权快速联合 方法在 C# windows 窗体应用程序中实现蒙特卡罗模拟。首先,我采用 gridSize,然后生成一个等于 gridSize * gridSize 的网格,由代表 sites 的黑色背景标签组成,然后我 随机联合 一些网站并将它们的背景设为白色。问题是每当我运行项目时,我都会得到黑色的整个站点 - 无论我尝试多少次 - ,但是如果我添加断点并逐步进行,我会得到一些白色的站点作为有意的!!!

代码如下:

public partial class Form1 : Form
{
int gridSize = 0;
byte siteSize = 50;
FlowLayoutPanel flowLayoutPanel;
UnionFind uf;
int randomFirstNumber;
int randomSecondNumber;
string labelName = string.Empty;
Random rnd = new Random();
int numberOfAttempts;
Label label;
HashSet<int> excludeHashSet = new HashSet<int>();

public Form1()
{
InitializeComponent();
}

private void setGridSizeButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(gridSizeTextbox.Text) && int.TryParse(gridSizeTextbox.Text, out gridSize))
{
if (gridSize > 0)
{
var flowLayoutPanel = createFlowLayoutPanelAndLabels();

setGridSizeButton.Enabled = false;
gridSizeTextbox.Enabled = false;

randomlyUnionSitesAndWhiteThem();
}
}
}

private void reset_Click(object sender, EventArgs e)
{
setGridSizeButton.Enabled = true;
gridSizeTextbox.Enabled = true;
gridSizeTextbox.Text = string.Empty;
flowLayoutPanel.Dispose();
excludeHashSet = new HashSet<int>();
gridSizeTextbox.Focus();
}

private FlowLayoutPanel createFlowLayoutPanelAndLabels()
{
flowLayoutPanel = new FlowLayoutPanel();
flowLayoutPanel.Name = "flowLayoutPanel";
flowLayoutPanel.Location = new Point(30, 60);
flowLayoutPanel.Size = new Size(gridSize, gridSize);
flowLayoutPanel.BorderStyle = BorderStyle.Fixed3D;
flowLayoutPanel.Width = siteSize * gridSize + 10 * gridSize;
flowLayoutPanel.Height = siteSize * gridSize + 10 * gridSize;

for (int i = 0; i < gridSize * gridSize; i++)
{
var label = new Label();
label.Text = "";
label.Name = i.ToString();
label.Width = siteSize;
label.Height = siteSize;
label.BackColor = Color.Black;
label.Margin = new Padding(3);
label.BorderStyle = BorderStyle.FixedSingle;

flowLayoutPanel.Controls.Add(label);
}
this.Controls.Add(flowLayoutPanel);

return flowLayoutPanel;
}

private void randomlyUnionSitesAndWhiteThem()
{
uf = new UnionFind(gridSize * gridSize);

numberOfAttempts = rnd.Next(0, gridSize * gridSize);
for (int i = 0; i < numberOfAttempts; i++)
{
randomFirstNumber = getRandomNumber(0, gridSize * gridSize, excludeHashSet);
randomSecondNumber = getRandomNumber(0, gridSize * gridSize, excludeHashSet);

labelName = uf.union(randomFirstNumber, randomSecondNumber).ToString();
if (Convert.ToInt16(labelName) > -1)
{
label = (Label)flowLayoutPanel.Controls.Find(labelName, false).First();
label.BackColor = Color.White;
excludeHashSet.Add(Convert.ToInt16(labelName));
}
}
}

private int getRandomNumber(int min, int max, HashSet<int> excludes)
{
int randomNumber;
do
{
var range = Enumerable.Range(min, max).Where(i => !excludes.Contains(i));

var rand = new Random();
int index = rand.Next(0, max - excludes.Count);

randomNumber = range.ElementAt(index);
}
while (excludes.Any(x => x == randomNumber));

return randomNumber;
}
}

这是我的 Union 方法:

class UnionFind
{
.....
public int union(int firstNumber, int secondNumber)
{
firstNumber = getRoot(firstNumber);
secondNumber = getRoot(secondNumber);

if (firstNumber == secondNumber)
{
return -1;
}

if (sizes[firstNumber] < sizes[secondNumber])
{
array[firstNumber] = secondNumber;
sizes[secondNumber] += sizes[firstNumber];

return array[firstNumber];
}
else
{
array[secondNumber] = firstNumber;
sizes[firstNumber] += sizes[secondNumber];

return array[secondNumber];
}
}
.....
}

最佳答案

您在 getRandomNumber 方法中声明了一个新的 Random 对象,它始终为您提供相同的值;如果您改用属性 rnd,它就可以工作。

Here is the explanation

关于c# - 在 C# 中实现蒙特卡洛模拟时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38618380/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com