简体   繁体   English

Windows Phone 8 C#随机椭圆生成

[英]Windows Phone 8 C# Random Ellipse generation

I'm new to building Windows Phone 8/8.1 apps (second app), and i'm having a pretty annoying problem. 我是构建Windows Phone 8 / 8.1应用程序(第二个应用程序)的新手,并且遇到了一个非常烦人的问题。

What i'm trying to do, it I want to randomly generate an ellipse on my Canvas. 我想做的是,我想在Canvas上随机生成一个椭圆。 But when i've generated the ellipse, I found out that the ellipse is always generated somewhere on the black line, as you can see in the image down below: 但是当我生成椭圆时,我发现椭圆总是生成在黑线上的某个地方,如下面的图像所示:

在此处输入图片说明

The code that i use to generate the ellipse is this: 我用来生成椭圆的代码是这样的:

    private void CreateDots()
    {

        Random Top = new Random();
        Random Left = new Random();

        int TopPos = Top.Next(0, 390); // screen height
        int LeftPos = Left.Next(0, 800); // screen width

        Ellipse Dot =new Ellipse();
        Dot.Name = "Dot";
        Dot.Fill = new SolidColorBrush(Color.FromArgb(255, 0,0,0));
        Dot.Width = 50;
        Dot.Height = 50;

        Canvas.SetTop(Dot, TopPos);
        Canvas.SetLeft(Dot, LeftPos);

        Canvas.Children.Add(Dot);
    }

Does anybody now how to generate the ellipse anywhere on the screen, and not only on the black line? 现在有人在屏幕上的任何地方,不仅在黑线上生成椭圆吗? Thank you in advance for any help. 预先感谢您的任何帮助。

Don't create two Random instances like that. 不要像这样创建两个Random实例。 Create a single instance and then use it repeatedly. 创建一个实例,然后重复使用。

Furthermore, persist the Random instance between calls to CreateDots . 此外,在对CreateDots调用之间保留Random实例。 If you don't and you make calls in rapid succession, you may end up with two consecutive Random instances that have been seeded the same. 如果您不这样做,并且快速连续拨打电话,则最终可能会得到两个连续的Random实例,这些实例已被植入相同的种子。

Random is by default seeded with the current time. 默认情况下,“ Random是当前时间的种子。

Your ellipses all tend to lie on the line where Left and Top have the same value because the two Random instances are created in rapid succession and almost always seeded with the same current time. 您的椭圆都倾向于位于“ Left和“ Top具有相同值的线上,因为两个“ Random实例是快速连续创建的,并且几乎总是在同一时间播种。 When you then generate the LeftPos and TopPos , both calls to Next are starting from the same state. 然后,当您生成LeftPosTopPos ,对Next两个调用都从相同的状态开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM