生成一个新的随机! [英] Generating a new random!
本文介绍了生成一个新的随机!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好codeproject复活节快乐,
我目前正在用随机块生成块,但是,
当我这样做时,整个大块是草,泥土,石头,或空气,
我不知道为什么,这就是我来这里的原因。新随机数总是保持不变
。我不知道为什么,但这令人沮丧,这是我的代码:
/ * 循环通过块来生成。 * /
for ( int i = 0 ; i < Chunk.ChunkWidth; i ++)
{
for ( int j = 0 ; j < Chunk.ChunkHeight; j ++)
{
if (Chunk.ChunkY > 0 )
{
int r = < span class =code-keyword> new Random()。Next( 0 , 4 );
/ * 将材料设置为污垢。 * /
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
{
/ * 将块材料设置为空气。 * /
Block.MakeBlock(BlockMaterial.Material_Air);
}
/ * 设置块。 * /
Chunk.SetBlock(i,j,Block);
}
}
}
解决方案
嗨
试试这个:
var randomNumber = new Random();
/ * 循环通过块来生成。 * /
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 );
/ * 将材料设置为污垢。 * /
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
{
/ * 将块材料设置为空气。 * /
Block.MakeBlock(BlockMaterial.Material_Air);
}
/ * 设置块。 * /
Chunk.SetBlock(i,j,Block);
}
}
}
尝试在循环外生成随机数。
/ * 循环通过块来生成。 * /
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 );
/ * 将材料设置为污垢。 * /
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
{
/ * 将块材料设置为空气。 * /
Block.MakeBlock(BlockMaterial.Material_Air);
}
/ * 设置块。 * /
Chunk.SetBlock(i,j,Block);
}
}
}
私有 int RandomNumber( int min, int max)
{
随机随机= new Random();
return random.Next(min,max);
}
调试程序并检查RandomNumber
函数返回的内容
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:
/* 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);
}
}
}
解决方案
Hi
Try this:
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); } } }
Try to generate random numbers outside the loop.
/* 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 whatRandomNumber
function returns.
这篇关于生成一个新的随机!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文