0

你好

我想在一个大椭圆上画 40 个椭圆。真奇怪。

for (int i = 0; i < 40; i++)
        {
            {
                {
                    Random r = new Random();
                    int distance = r.Next(0, 10000);
                    var rv = r.Next(0, 359);
                    var x = Math.Sin(rv * Math.PI / 180) * 225;
                    rv = r.Next(0, 359);
                    var y = Math.Cos(rv * Math.PI / 180) * 225;
                    Ellipse e = new Ellipse();
                    e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
                    e.Margin = new Thickness(y, -150 + x, 0, 0);
                    e.Width = 25;
                    e.Height = 25;
                    counter++;
                    PointsGrid.Children.Add(e);
                }
            }
        }

此代码仅绘制 7,8 或有时 9 个椭圆。但是,如果我在代码中添加额外的行:

for (int i = 0; i < 40; i++)
        {
            {
                {
                    Random r = new Random();
                    int distance = r.Next(0, 10000);
                    var rv = r.Next(0, 359);
                    var x = Math.Sin(rv * Math.PI / 180) * 225;
                    rv = r.Next(0, 359);
                    var y = Math.Cos(rv * Math.PI / 180) * 225;
                    Ellipse e = new Ellipse();
                    e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
                    e.Margin = new Thickness(y, -150 + x, 0, 0);
                    e.Width = 25;
                    e.Height = 25;
                    counter++;
                    PointsGrid.Children.Add(e);
                    MessageBox.Show(""); // Additional line
                }
            }
        }

如果我添加显示消息框,在所有消息框上按确定,我将看到所有 40 个省略号...

问题是它怎么可能,我该如何解决?

4

2 回答 2

4

问题是您使用随机。如果您每次都创建一个新的 Random() - 它将使用当前时间作为种子。由于当前时间是相同的(因为代码执行得如此之快,所以伪随机数总是相同的。)

Random r = new Random();

for (int i = 0; i < 40; i++)
        {
            {
                {
                    int distance = r.Next(0, 10000);
                    var rv = r.Next(0, 359);
                    var x = Math.Sin(rv * Math.PI / 180) * 225;
                    rv = r.Next(0, 359);
                    var y = Math.Cos(rv * Math.PI / 180) * 225;
                    Ellipse e = new Ellipse();
                    e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
                    e.Margin = new Thickness(y, -150 + x, 0, 0);
                    e.Width = 25;
                    e.Height = 25;
                    counter++;
                    PointsGrid.Children.Add(e);
                    //MessageBox.Show(""); // Additional line
                }
            }
        }

消息框只是“减慢”了执行速度——这意味着你所有的圈子都没有被画在彼此的顶部。

于 2012-04-27T10:06:28.783 回答
3

那可能是时间问题。您应该在循环之外声明 Random,因为种子是基于当前时间的。

    Random r = new Random();

    for (int i = 0; i < 40; i++)
    {
        int distance = r.Next(0, 10000);
        var rv = r.Next(0, 359);
        var x = Math.Sin(rv * Math.PI / 180) * 225;
        rv = r.Next(0, 359);
        var y = Math.Cos(rv * Math.PI / 180) * 225;
        Ellipse e = new Ellipse();
        e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
        e.Margin = new Thickness(y, -150 + x, 0, 0);
        e.Width = 25;
        e.Height = 25;
        counter++;
        PointsGrid.Children.Add(e);
    }

如果这真的是你的问题,那就意味着你真的画了 40 个椭圆,但其中大部分是重叠的。

于 2012-04-27T10:06:16.867 回答