r/code • u/skeetermcbeater • Aug 05 '24
Help Please Need help planning out a rarity system
I need help creating a rarity system for items in a TTRPG game. I’d like to integrate the system with my website using JavaScript. So far, I’ve been able to create a random item generator that selects items from different arrays and puts them together into a newly formed array, then displayed to the user. Now, I need to assign rarity values (Very rare, common, Very common) with corresponding percent values (5%, 45%, 55%).
I’ve got a few ideas for how to create the program but I’m still new to coding and only have a rudimentary knowledge of JavaScript. How would I assign rarity values to different items, then apply the percent values to the corresponding rarity and generate an array/list?
•
u/waozen Aug 05 '24 edited Aug 06 '24
Friendly reminder. Please review the subreddit rules.
New posts should contain or link to code. Thank you.
4
u/karlosvas Aug 05 '24
If the rarity system has to do with the weapons you have, it's as easy as dividing. If you have 1 very rare weapon, 10 common and 100 very common, the rarity percentage of a very rare is 111/1= 11% Total weapons/Weapons of that type
You could also make an object with a key of the rarity types and values with an array of the weapon types that have that rarity, each of them is a key and its value its rarity, you access object.vrare and it returns the array of very rare objects. Every time you add a very rare, so that it has a rarity in that range, let newWeapon = { ormagedon: Math.floor(Math.random() * num_max_rare) }; object.vrare.push(newWeapon);
All that if you prefer vanilla javascript, if not, as they have commented, there are libraries for it.
3
u/angryrancor Boss Aug 05 '24
Consider using faker.js to provide your randomized attributes. Everything is covered in the first page of the documentation linked.
Edit: The "percent values" (weights) can be done with this: https://fakerjs.dev/api/helpers.html#weightedarrayelement
1
u/Fvgsgygyy Apr 21 '25
You can actually solve this incredibly easily with some math knowledge. Im pretty well versed in probability, but I feel this method is universally understandable. First, identify how rare you Want things to be. If the rarest thing you can get it 10%, use 100 as the base number(I’ll explain what this means). Higher rarity calls for a higher base number. Dont use weird numbers, stick to powers of ten. Then, pick a number for this item to be assigned to. Let’s say this number is 1, and our base number 100. Then, make a variable, and make it a random number between 1 and 100 (make sure to put this after a mouse clicked script, so it’s repeatable and the picked number doesn’t remain constant):
local target number = 1 local pickednumber = math.random(1,100) if pickednumber == target number then
local picked number = math.random (1,100) if 1 <= pickednumber <= 36