<分区>
函数定义如下:
/**
* @param {number} x The x-coordinate (can be positive or negative)
* @param {number} y The y-coordinate (can be positive or negative)
* @param {number} tileCount The number of available tiles
* @return {number} The selected tile index
*/
function getRandomTileIndex(x, y, tileCount) {
// Fill in code here
}
例如,我可以 return x * y % tileCount
但我想引入随机性。我可以做 return Math.round(Math.random() * (tileCount-1))
但是每次都会返回不同的图 block 索引。
我希望这个函数是确定性的,所以当使用相同的输入 (x, y, tileCount)
时,总是会出现相同的输出。但我也希望它看起来(尽可能)随机且分布均匀 - 随机性的质量不一定是完美的。
这个随机图 block 生成器的目的是用于具有(几乎)无限网格的游戏 - 用户从中间 (x,y) = (0,0)
开始,然后向外移动无论他想要什么方向 - 我只会为“地面”提供固定数量的背景图 block - 我想要它以便每次加载游戏时世界看起来都一样。