JavaScript rarity algorithm not functioning properly for NFT generation project

I’m working on an NFT collection similar to CryptoPunks with my kid. We’re trying to build 10,000 unique character images using different traits that get combined randomly.

We found a code generator that mostly works but we’re having trouble with the rarity system. Some traits should appear way less often than others but right now everything seems to show up equally.

Here’s our rarity setup in the config file:

const rarityLevels = [
  { identifier: "", type: "common" },
  { identifier: "_uncommon", type: "uncommon" },
  { identifier: "_legendary", type: "legendary" }
];

module.exports = { traitOrder, imageFormat, rarityLevels, baseCollection };

I tried naming our image files like “trait_legendary.png” but the common traits still generate just as much as the legendary ones. The frequency distribution isn’t working at all.

Has anyone dealt with weighted randomness for NFT trait generation before? We’re stuck and would really appreciate any guidance on making certain attributes more rare than others.

Your configuration is only defining the naming patterns but there’s no actual weighting mechanism being applied during generation. The rarity system needs two components: the identification part (which you have) and the probability distribution logic (which appears to be missing). I ran into this exact issue when building my first generative collection last year. The solution was implementing a weighted random function that reads the filename identifiers and maps them to probability values. You’ll need something that calculates cumulative weights - so if common has weight 70, uncommon 25, and legendary 5, your random function should generate a number between 0-100 and select traits based on which range it falls into. Most importantly, make sure your generation loop is actually calling this weighted selection function instead of just picking randomly from the file directory. Without seeing your main generation code it’s hard to pinpoint exactly where the disconnect is happening, but the config alone won’t enforce rarity without proper implementation in the selection logic.

looks like ur missing the actual weight values in ur config. just having the identifiers wont make them rare auto - u need to specify probabilities for each tier like legendary: 0.05, uncommon: 0.2, common: 0.75 or something similar then use those weights in ur random generation logic

oh interesting project! i’m curious tho - what does the actual generation logic look like? the config you showed us seems to only define the naming convention but im wondering if there’s another part of the code that’s supposed to read these rarity levels and apply them during the random selection process?

like, when the generator picks which trait to use, is it just doing a basic random selection from all available files? because if thats the case then yeah, it would totally ignore the rarity identifiers even if they’re named correctly.

i’ve seen some nft generators where you need to set up a separate function that checks the filename identifier and then uses weighted random selection based on that. have you checked if there’s a generateTrait() or similar function somewhere that should be handling the probability calculations?

also just curious - how many trait files do you have for each rarity tier? sometimes the distribution can look weird if you have like 50 common traits but only 2 legendary ones, even with proper weighting.