我正在为一个小项目开设一个小类。这是一款基于文本的 RPG 游戏,我正在尝试为 NPC 死亡创建一个掉落类别。我创建了几个 Math.random 方法(它们完全相同,只是为了方便而命名不同。)来删除项目 id(名称)的随机数量并获取删除的项目的稀有度。一切正常,但它只随机化一次(在启动或运行时),并且不会随机化此后的金额。我还将其随机化在 2 个数字之间,例如 25 和 50,随机数不会低于 25 或高于 50。
我的问题是:每次NPC死亡后,如何随机化二维数组或普通数组中的整数,以便首先获得的随机数发生变化并且不会保持不变。因为现在,它停留在选择的数字上。如果数量是25,那么我杀死的下一个npc,数量仍然是25..和25..和25..等等。我需要它来随机化或改变。</p>
请帮忙,谢谢。
public class DropConfig {
private static final int
ALWAYS = 0,
VERY_COMMON = rate(1, 9),
COMMON = rate(10, 20),
UNCOMMON = rate(30, 40),
RARE = rate(50, 60),
VERY_RARE = rate(70, 80),
SUPER_RARE = rate(90, 100);
public static final int[][] NPC_DROPS = {
// Normal NPC's
{1, 526, 1, ALWAYS},
{2, 526, 1, ALWAYS},
{3, 526, 1, ALWAYS},
{1, 995, drop(1, 50), ALWAYS},
{2, 995, drop(1, 50), ALWAYS},
{3, 995, drop(1, 50), ALWAYS},
// Moderate NPC's
{9, 526, 1, ALWAYS},
{9, 995, drop(250, 500), UNCOMMON},
{9, 555, drop(2, 7), VERY_COMMON},
{9, 995, drop(5, 50), VERY_COMMON},
{9, 1050, 1, RARE},
};
public static int rate(int min, int max) {
return 1 + (int)(Math.random() * ((max - min) + 1));
}
//Same as rate, different name for looks.
public static int drop(int min, int max) {
return 1 + (int)(Math.random() * ((max - min) + 1));
}
这是我调用 Drops 方法的地方
public void npcDeath() {
int npc = 0;
if (npc == null)
return;
for(npc = 0; npc < DropConfig.NPC_DROPS.length; npc++) {
if(npc == DropConfig.NPC_DROPS[npc][0]) {
if(Misc.random(DropConfig.NPC_DROPS[npc][3]) == 0) { //Drops ALWAYS item
Item(DropConfig.NPC_DROPS[npc][1], DropConfig.NPC_DROPS[npc][2]);
}
}
}
}
最佳答案
如果我理解正确的话,您希望在每次使用此 NPC_DROPS 数组时重新初始化通过调用 drop()
初始化的 NPC_DROPS 数组的元素。
嗯,NPC_DROPS 是一个常量,所以它不能改变。每次访问时生成它,使用以下方法:
public static int[][] generateNpcDrops(){
return new int[][] {
// Normal NPC's
{1, 526, 1, ALWAYS},
{2, 526, 1, ALWAYS},
{3, 526, 1, ALWAYS},
{1, 995, drop(1, 50), ALWAYS},
{2, 995, drop(1, 50), ALWAYS},
{3, 995, drop(1, 50), ALWAYS},
// Moderate NPC's
{9, 526, 1, ALWAYS},
{9, 995, drop(250, 500), UNCOMMON},
{9, 555, drop(2, 7), VERY_COMMON},
{9, 995, drop(5, 50), VERY_COMMON},
{9, 1050, 1, RARE},
}
}
...
public void npcDeath() {
int npc = 0;
if (npc == null)
return;
int[][] npcDrops = DropConfig.generateNpcDrops();
for(npc = 0; npc < npcDrops.length; npc++) {
if(npc == npcDrops[npc][0]) {
if(Misc.random(npcDrops[npc][3]) == 0) { //Drops ALWAYS item
Item(c, npcDrops[npc][1], npcDrops[npc][2]);
}
}
}
}
关于java - 在随机化的数组中使用数字随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10395748/