Click here to Skip to main content
15,887,325 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello codeproject happy easter,

I am currently generating chunks with random blocks, however,
when I do this, the whole chunk is either grass, dirt, or stone, or air,
I am not sure why and that's why I came here. The new random always stays the same
in the void. I don't know why but it's frustrating, here is my code:

C#
/* Loop through the chunk to generate. */
    for (int i = 0; i < Chunk.ChunkWidth; i++)
    {
        for (int j = 0; j < Chunk.ChunkHeight; j++)
        {
            if (Chunk.ChunkY > 0)
            {
                int r = new Random().Next(0, 4);

                /* Set the material to dirt. */
                if (r == 0)
                    Block.MakeBlock(BlockMaterial.Material_Dirt);
                if (r == 1)
                    Block.MakeBlock(BlockMaterial.Material_Grass);
                if (r == 2)
                    Block.MakeBlock(BlockMaterial.Material_Stone);


            }
            else
            {
                /* Set the block material to air. */
                Block.MakeBlock(BlockMaterial.Material_Air);
            }

            /* Set the block. */
            Chunk.SetBlock(i, j, Block);
        }
    }
}
Posted
Comments
CPallini 31-Mar-13 15:29pm    
What's the value of Chunk.ChunkY?
Why are you generating a random number in the range of 0,1,2,3 (inclusive) while you need just 0,1,2 ?
Yvar Birx 31-Mar-13 15:34pm    
Chunk.ChunkY is the cooardinate of the Chunk, this is a way for me to only generate below the walking level, it's a bit like Flatgrass in Minecraft really. Oh, yeh, I am, I always forget about the fact that everything starts at zero not at one, however, the fact that the chunks are being generated like this:

http://i48.tinypic.com/ws5h5e.png


Has nothing to do with any of that. :L

Also, the colors are pretty much squares (32*32), which all show another chunk. So, you can see that the blocks are not random and I don't understand why.

Hi
Try this:

C#
var randomNumber = new Random();

/* Loop through the chunk to generate. */
    for (int i = 0; i < Chunk.ChunkWidth; i++)
    {
        for (int j = 0; j < Chunk.ChunkHeight; j++)
        {
            if (Chunk.ChunkY > 0)
            {
                var r =randomNumber.Next(0, 4);
 
                /* Set the material to dirt. */
                if (r == 0)
                    Block.MakeBlock(BlockMaterial.Material_Dirt);
                if (r == 1)
                    Block.MakeBlock(BlockMaterial.Material_Grass);
                if (r == 2)
                    Block.MakeBlock(BlockMaterial.Material_Stone);
 

            }
            else
            {
                /* Set the block material to air. */
                Block.MakeBlock(BlockMaterial.Material_Air);
            }
 
            /* Set the block. */
            Chunk.SetBlock(i, j, Block);
        }
    }
}
 
Share this answer
 
Comments
Yvar Birx 31-Mar-13 15:07pm    
Funnily enough I already tried that, anyways, here is a picture to give you an idea what messes up:

http://i48.tinypic.com/ws5h5e.png
Yvar Birx 31-Mar-13 15:10pm    
Oh yeah, also,

The different colors are the blocks in the chunks, as you can see there are three different chunks yet they all have the same block.
Shahin Khorshidnia 31-Mar-13 17:43pm    
I think the problem is already solved ;)
Maciej Los 31-Mar-13 15:47pm    
Initializing random outside the loop, should do the job.
+5!
Yvar Birx 31-Mar-13 15:55pm    
How unfortunate that it didn't. :L
Try to generate random numbers outside the loop.
C#
/* Loop through the chunk to generate. */
    for (int i = 0; i < Chunk.ChunkWidth; i++)
    {
        for (int j = 0; j < Chunk.ChunkHeight; j++)
        {
            if (Chunk.ChunkY > 0)
            {
                int r = RandomNumber(0, 4);

                /* Set the material to dirt. */
                if (r == 0)
                    Block.MakeBlock(BlockMaterial.Material_Dirt);
                if (r == 1)
                    Block.MakeBlock(BlockMaterial.Material_Grass);
                if (r == 2)
                    Block.MakeBlock(BlockMaterial.Material_Stone);


            }
            else
            {
                /* Set the block material to air. */
                Block.MakeBlock(BlockMaterial.Material_Air);
            }

            /* Set the block. */
            Chunk.SetBlock(i, j, Block);
        }
    }
}

private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}


Debug your program and check what RandomNumber function returns.
 
Share this answer
 
Comments
Yvar Birx 31-Mar-13 17:02pm    
Okay, whatever seems to be the problem it's not the random, I will do some research in my code, I thank you all for the help.
Maciej Los 31-Mar-13 17:50pm    
Good think. You're welcome ;)
Sergey Alexandrovich Kryukov 31-Mar-13 22:56pm    
That's the key, a 5.
—SA
Maciej Los 1-Apr-13 3:23am    
Thank you, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900