Tutorial-Blog Controlling RNG in Treasure Chests

Nice! For the scripted part, I'd personally consider something like this:
Spoiler: script call
JavaScript:
// Define IDs of eligible drops
const itemIds   = [7, 10, 11, 15];
const weaponIds = [];
const armorIds  = [];
// Get corresponding items
const drops = itemIds.map(id => $dataItems[id]).concat(
  weaponIds.map(id => $dataWeapons[id]),
  armorIds.map(id => $dataArmors[id])
);
// Count those items in inventory
const count = drops.map(item => $gameParty.numItems(item));
// Get drop rates (high count => low chance)
const maxCount = Math.max(...count);
const rates = count.map(n => 1 + maxCount - n);
// Random selection weighted by rates
const total = rates.reduce((a, c) => a + c, 0);
let r = 1 + Math.randomInt(total);
const index = rates.findIndex(a => (r -= a) <= 0);
const drop  = drops[index];
// Store item's name and icon index for message
$gameVariables.setValue(1, drop.iconIndex);
$gameVariables.setValue(2, drop.name);
// Gain item
$gameParty.gainItem(drop, 1);
[Edit: added +1 to r so it fits the correct range (1 ~ total).]
I like the added scripting tutorial a lot, it's a great help!
Always need some good pointers for RNG. Thank you~! And @caethyril, thank you! A good script would help a lot. ( - u -~<3
thanks
really cool idea!
Nice! For the scripted part, I'd personally consider something like this:
Spoiler: script call
JavaScript:
// Define IDs of eligible drops
const itemIds   = [7, 10, 11, 15];
const weaponIds = [];
const armorIds  = [];
// Get corresponding items
const drops = itemIds.map(id => $dataItems[id]).concat(
  weaponIds.map(id => $dataWeapons[id]),
  armorIds.map(id => $dataArmors[id])
);
// Count those items in inventory
const count = drops.map(item => $gameParty.numItems(item));
// Get drop rates (high count => low chance)
const maxCount = Math.max(...count);
const rates = count.map(n => 1 + maxCount - n);
// Random selection weighted by rates
const total = rates.reduce((a, c) => a + c, 0);
let r = 1 + Math.randomInt(total);
const index = rates.findIndex(a => (r -= a) <= 0);
const drop  = drops[index];
// Store item's name and icon index for message
$gameVariables.setValue(1, drop.iconIndex);
$gameVariables.setValue(2, drop.name);
// Gain item
$gameParty.gainItem(drop, 1);
[Edit: added +1 to r so it fits the correct range (1 ~ total).]
Thank you, this is very useful.
I was looking for an RNG that will give items based on predetermined rates. Item one drops 50% of the times, item two drops 20% of the time and item three drops whenever the other items dont drop. Hopefully that makes sense I am pretty new to all of this.
@imDARR3N - you can do that with eventing. Use the Control Variables command to set a variable equal to a random number, e.g. 1~100. Then check the value of that variable using Conditional Branch commands. E.g.
Spoiler: click to see event example
◆Control Variables:#0001 Check = Random 1..100 ◆If:Check ≤ 50 ◆Text:None, None, Window, Bottom : :Rolled \v[1], range [1, 50], 50% chance! ◆ :Else ◆If:Check ≤ 70 ◆Text:None, None, Window, Bottom : :Rolled \v[1], range [51, 70], 20% chance! ◆ :Else ◆Text:None, None, Window, Bottom : :Rolled \v[1], range [71, 100], 30% chance! ◆ :End ◆ :End
This example just uses Show Text to show the results, but obviously you can replace that with Change Items or whatever you like~

[Edit: rephrased Show Text messages in example for better clarity.]
@imDARR3N - you can do that with eventing. Use the Control Variables command to set a variable equal to a random number, e.g. 1~100. Then check the value of that variable using Conditional Branch commands. E.g.
Spoiler: click to see event example
◆Control Variables:#0001 Check = Random 1..100 ◆If:Check ≤ 50 ◆Text:None, None, Window, Bottom : :Rolled \v[1], range [1, 50], 50% chance! ◆ :Else ◆If:Check ≤ 70 ◆Text:None, None, Window, Bottom : :Rolled \v[1], range [51, 70], 20% chance! ◆ :Else ◆Text:None, None, Window, Bottom : :Rolled \v[1], range [71, 100], 30% chance! ◆ :End ◆ :End
This example just uses Show Text to show the results, but obviously you can replace that with Change Items or whatever you like~

[Edit: rephrased Show Text messages in example for better clarity.]
Thank you!
Thanks for this!
nice!! this is something i've been thinking about but could never wrap my head around doing, thanks for the tutorial!

Original thread: https://forums.rpgmakerweb.com/threads/controlling-rng-in-treasure-chests.151441/