1

I'm trying to create a procedural cave generator and so far I have the code for generating a completely 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");
        }
    }
}

The image I'm trying to obtain is something like this, except in black and white:

desired image

What I'm getting is this:

generated image

I believe it's because of the random number generator I'm using (i.e. just Random().NextDouble()).
Anyone know a better RNG?

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • 1
    You're creating a new RNG for each column. Create just one for your whole program. The reason for the artifact on the right third of your image is that you are constructing `Random` with no seed, which implicitly uses `Environment.TickCount` as a seed, which happened to have changed at that column. – Rotem May 27 '18 at 19:21

1 Answers1

2

Instead of using Random rng = new Random(); inside the for loop, create a single instance above, and use the same instance in each loop. This is because multiple Random instances created in quick succession will have the same seed and therefore generate the same sequence of pseudorandom numbers.

For example:

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;
    }
    ...
}
Callum Watkins
  • 2,844
  • 4
  • 29
  • 49