I've been stuck this evening on getting a Perlin Noise function to generate by itself. Every time I run the program without adding to different types of noise together it calls an ArgumentException error.

Here is my code I'm using.

        //perlin gen
        Random rand = new Random();
        double freq = 1 / 32.0;
        double lacu = 3.0;
        int octave = 5;
        double pers = 0.5;
        int seed = 1;

        Perlin perlin = new Perlin(freq, lacu, pers, octave, seed, QualityMode.High);

        //init the noise map
        noiseMap = new Noise2D(128, 128);
        noiseMap.GeneratePlanar(1, 2, 1, 2, true);

And the error that it calls is here.

public void GeneratePlanar(double left, double right, double top, double bottom, bool seamless)
        if (right <= left || bottom <= top || this.m_generator == null)
            throw new ArgumentException();

I've checked and re-checked the code multiple times, so I'm not sure what is going on that is making the GeneratePlanar function call the ArgumentException error.


2 Answers 2


Place a breakpoint on throw new ArgumentException();. When it breaks check the values of right, left, bottom, top and m_generator. See which one is the problem. Is m_generator null? Does right == left? When the debugger breaks, you can see the call stack. The call stack will show you all the function calls that have lead to this function being called.

The debugger is just about the programmers best friend. The sooner you learn to use it the better. See this post I wrote for more details about debuggers.

I'm assuming that this.m_generator is null, since I see from your code that the other values are correct. Look in the code an see where m_generator gets set, then you'll know what you need to do to set it properly.

  • \$\begingroup\$ Ah, thanks so much friend! However I have one more question to ask. When i use the Noise2D function in my code it happens to take one more argument. In the deceleration of the Noise2D function there is room for the m_generator argument and when I try and add that to my code where I want to generate the noise I get errors. Here is the deceleration of the function Noise2D public Noise2D(int width, int height, ModuleBase generator) { this.m_generator = generator; this.m_width = width; this.m_height = height; this.m_data = new float[width, height]; } \$\endgroup\$ Jul 20, 2012 at 2:31
  • \$\begingroup\$ and here is my code to use it. noiseMap = new Noise2D(128, 128, ModuleBase generator); Not sure if I am using it the right way. \$\endgroup\$ Jul 20, 2012 at 2:33
  • \$\begingroup\$ Nope, you'll want to do something like noiseMap = new Noise2D(128, 128, new ModuleBase(whatever arguments this needs)); You're getting close! \$\endgroup\$
    – House
    Jul 20, 2012 at 2:38
  • \$\begingroup\$ Thanks Byte56, I figured it out with your help! Here is a screenpic of the noise :) \$\endgroup\$ Jul 20, 2012 at 3:06
  • \$\begingroup\$ That's some perfect noise! Nice work. \$\endgroup\$
    – House
    Jul 20, 2012 at 3:12

Where is m_generator coming from? I don't see it listed in the rest of your code provided, so is it initialized somewhere else?

You could also put some debug prints just before throwing the exception, and seeing what the values you have for those variables are. Getting an idea why you're in there would be a start to fixing it.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .