首页 > 解决方案 > 为什么随机数没有正确分散?

问题描述

我的任务是使用 LCG 算法实现一个随机数生成器。

任务是在 [-1, 1] 之间生成 1000 个轴 (x,y) 并将它们打印在窗格上。
如果该点在半径为 1.0 的圆内,它将被打印为红色。
否则,蓝。

我使用了这个 YouTube 视频中建议的数值食谱建议的参数。我遵循此链接中使用的编码风格。

我正在使用ZedGraph来显示我的图。

为什么随机数没有正确分散在窗格上?
而且,蓝点在哪里?

随机数生成器类:

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

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

    public 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;
    }
}

输出

在此处输入图像描述


附加源代码:

驱动程序:

class Program
{
    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);

        MyRandom r = new MyRandom();

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

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

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

        //// get a reference to the GraphPane
        GraphPane gPane = zgControl.GraphPane;
        gPane.Title.Text = "Random Numbers";
        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 代码:

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

        ZedGrapgControl = this.zgc;
    }
}

标签: c#random

解决方案


seed不应该是随机函数的参数,而是随机类中的一个字段。你设置一次,它会随着每次随机调用而改变。

但是要回答您的问题,您不会new_seed在任何地方保存。它必须被保存,以便在下一次随机调用中使用。因此,您的种子在每次新调用中都会增加 1,这使得图形成为一条直线。


推荐阅读