c# - 任何人都知道一个好的随机数生成器

标签 c# random

<分区>

我正在尝试创建一个程序化的洞穴生成器,到目前为止我已经有了生成完全随机 map 的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace CelularAutomata
{
    class Program
    {
        static int width=512, height=512;
        Boolean[,] cellmap = new Boolean[width, height];
        static float chanceToStartAlive = 0.45f;

        public static Boolean[,] initialiseMap()
        {
            Boolean[,] map = new Boolean[width, height];

            for (int x = 0; x < width; x++)
            {
                Random rng = new Random();
                for (int y = 0; y < height; y++)
                {
                    doube random = rng.NextDouble();
                    if (random < chanceToStartAlive)
                    {
                        map[x,y] = true;
                    }
                }
            }
            return map;
        }

        public static Bitmap createImage(Boolean[,] map)
        {
            Bitmap bmp = new Bitmap(512, 512);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (map[x, y])
                    {
                        bmp.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                    }
                    else 
                    {
                        bmp.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    }
                 }
                Console.Write('\n');
            }
            return bmp;
        }

        static void Main(string[] args)
        {
            Boolean[,] map = initialiseMap();
            Bitmap bmp = createImage(map);
            bmp.Save("C:\\Users\\radu\\Documents\\Sync\\Licenta\\chamber.png");
        }
    }
}

我要获取的图像是这样的,除了黑白:

desired image

我得到的是这样的:

generated image

我相信这是因为我使用的随机数生成器(即只是 Random().NextDouble())。
有人知道更好的 RNG 吗?

最佳答案

不要在 for 循环中使用 Random rng = new Random();,而是在上面创建一个实例,并在每个循环中使用相同的实例。这是因为快速连续创建的多个 Random 实例将具有相同的种子,因此会生成相同的伪随机数序列。

例如:

class Program
{
    Random rng = new Random();
    ...
    public static Boolean[,] initialiseMap()
    {
        Boolean[,] map = new Boolean[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                double random = rng.NextDouble();
                if (random < chanceToStartAlive)
                {
                    map[x,y] = true;
                }
            }
        }
        return map;
    }
    ...
}

关于c# - 任何人都知道一个好的随机数生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555681/

相关文章:

c# - 在线程池外创建线程有什么好处?

c# - 使用 double 组时内存泄漏

c# - 来自 Mosha Pasumansky 的 XAML 数据透视表

javascript - 如何从 JSON 字典中按键检索随机 JSON 对象?

c# - Random.Next() 的概率

Haskell 函数生成随机数,每次该数字都与前一个不同

c# - 二进制搜索算法出现错误 - 使用未分配的局部变量

javascript - 如何在 JavaScript 中根据距头部圆的距离生成新的随机圆?

ruby-on-rails - rails 3 : Get Random Record

c# - 如何按数据库查询结果中的列值汇总结果