Rumah  >  Artikel  >  hujung hadapan web  >  n X Pengganda Kebarangkalian

n X Pengganda Kebarangkalian

王林
王林asal
2024-07-17 11:53:46659semak imbas

n X Probability Multiplier

Saya cuba membuat sistem kebarangkalian 1 dalam X, tetapi atas sebab tertentu pengganda tidak berfungsi. Saya memberi diri saya pengganda tinggi dan saya masih mendapat blok biasa.

const blocks_rng = [
  { name: "Dirt Block", item: "dirt", chance: 2 },
  { name: "Farmland", item: "farmland", chance: 3 },
  { name: "Oak Log", item: "oak_log", chance: 4 },
  { name: "Andesite", item: "andesite", chance: 6 },
  { name: "Granite", item: "granite", chance: 9 },
  { name: "Diorite", item: "diorite", chance: 12 },
  { name: "§9Stone", item: "stone", chance: 16 },
  { name: "§9Amethyst", item: "amethyst_block", chance: 32 },
  { name: "§9Magma", item: "magma", chance: 64 },
  { name: "§9Enchanting Table", item: "enchanting_table", chance: 128 },
  { name: "§9Mob Spawner", item: "mob_spawner", chance: 250 },
  { name: "§9Obsidian", item: "obsidian", chance: 512 },
  { name: "§dCrying Obsidian", item: "crying_obsidian", chance: 1024 },
  { name: "§dBeacon", item: "beacon", chance: 8024 },
  { name: "§dEnd Frame", item: "end_portal_frame", chance: 2500 },
  { name: "§dBedrock", item: "bedrock", chance: 5000 },
  { name: "§5Command Block", item: "command_block", chance: 10000 },
  { name: "§5Chain Command Block", item: "chain_command_block", chance: 25000 },
  { name: "§5Repeating Command Block", item: "repeating_command_block", chance: 30000 },
  { name: "§4§l§k!§4§l???§r§4§l§k!", item: "stone", chance: 999999999 }
];

function getRandomBlock() {
  const mult = 20;
  const scaledChances = blocks_rng.map(block => mult / block.chance);
  const totalScaledChance = scaledChances.reduce((sum, scaledChance) => sum + scaledChance, 0);

  let random = Math.random() * totalScaledChance;
  for (let i = 0; i < blocks_rng.length; i++) {
    if (random < scaledChances[i]) {
      return blocks_rng[i];
    }
    random -= scaledChances[i];
  }

  return blocks_rng[blocks_rng.length - 1];
}

Atas ialah kandungan terperinci n X Pengganda Kebarangkalian. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn