samedi 14 mars 2020

Why are the random numbers not properly scattered?

My task is to generate 1000 axes (x,y) between [-1, 1] and print them on a pane.
If the point is inside the circle of radius 1.0, it will be printed as Red.
Otherwise, Blue.

I used the parameters suggested by Numerical Recipes suggested in this YouTube video. I am following the coding style used in this link.

Why are the random numbers not properly scattered on the pane?
And, where are the blue points?

    class Program
    {
        static long m = 4294967296;// modulus
        static long a = 1664525; // multiplier
        static long c = 1013904223; // increment
        static long nextRandomInt(long seed)
        {
            return (((a * seed + c) % m));
        }

        static double nextRandomDouble(long seed)
        {
            return (2*(nextRandomInt(seed) / m)) - 1;
        }

        static double nextRandomDouble(double seed)
        {
            double new_seed = seed + 1.0;
            new_seed = new_seed / 2.0;
            new_seed = new_seed * m;

            long long_seed = Convert.ToInt64(new_seed);

            double new_s = nextRandomInt(long_seed);
            new_s = new_s / m;
            new_s = new_s * 2;
            new_s = new_s - 1;

            return new_s;
        }

        static void Main(string[] args)
        {
            int N = 1000;
            double radius = 1.0;

            List<double> rx = new List<double>(); rx.Add(0.0);            
            List<double> ry = new List<double>(); ry.Add(1.0);

            for (int i = 0; i < N; i++)
            {
                double x = nextRandomDouble(rx[rx.Count - 1]);
                double y = nextRandomDouble(ry[ry.Count - 1]);

                rx.Add(x);
                ry.Add(y);

                Console.Write("<{0}, {1}>\t", x, y);
            }

            PlotForm form = new PlotForm();
            ZedGraphControl zgControl = form.ZedGrapgControl;

            //// get a reference to the GraphPane
            GraphPane gPane = zgControl.GraphPane;
            gPane.XAxis.Type = AxisType.Linear;

            PointPairList insideCircleList = new PointPairList();
            PointPairList outsideCircleList = new PointPairList();

            for (int i = 0; i < N; i++)
            {
                double x = rx[i];
                double y = ry[i];

                if ((x * x + y * y) < radius)
                {
                    insideCircleList.Add(x, y);
                }
                else
                {
                    outsideCircleList.Add(x, y);
                }
            }

            LineItem redCurve = gPane.AddCurve("Inside", insideCircleList, Color.Red, SymbolType.Circle);
            redCurve.Line.IsVisible = false;
            redCurve.Symbol.Fill.Type = FillType.Solid;

            LineItem blueCurve = gPane.AddCurve("Outside", outsideCircleList, Color.Blue, SymbolType.Circle);
            blueCurve.Line.IsVisible = false;

            zgControl.AxisChange();

            form.ShowDialog();

            Console.ReadLine();
        }
    }

WinForms Source Code:

public partial class PlotForm : Form
{
    public ZedGraph.ZedGraphControl ZedGrapgControl { get; set; }
    public PlotForm()
    {
        InitializeComponent();

        ZedGrapgControl = this.zgc;
    }
}

Output

enter image description here




Aucun commentaire:

Enregistrer un commentaire