Rarity Function
written by DrAbstract
RARITY
The rarity() function is a way to provide a list of options along with their frequency and receive back a randomized array of the options repeated based on their frequency. We usually then select the first element of the array to get the selected option.
// SIMPLE
// rarity() will return a randomized array with 20 reds, 70 greens and 10 blue
// the frequencies do not need to add to 100
// so frequencies of 2, 7 and 1 would be the same result
// color has a 20% chance of being red, 70% chance of green and 10% chance blue
const color = rarity({red:20, green:70, blue:10})[0];
// COMPLEX
// For (fx)hash we may want to use the name of the possibility
// but then use some other value in our code
// so we have added an optional payload system (a look-up table)
// the element format becomes name:[frequency, payload]
// the payload can be any object but see docs for handling arrays
const speed = rarity({slow:[10, 1], medium:[30, 5], fast[60, 10]})[0];
// the result is a String object not a String primitive
// this allows us to store the payload right on the result
// so if speed is slow then speed.payload is 1, etc.
Read more about rarity() in the ZIM docs and find out more about ZIM at https://zimjs.com - and making interactive NFTs with ZIM which includes an (fx)hash template.
FEATURES
(fx)hash has a function to receive features. This is an object literal that must contain primitives. So we have provided a makePrimitive() function to convert as follows:
window.$fxhashFeatures = makePrimitive({
"Color": color,
"Speed": speed
});
ENHANCE
The (fx)hash features offer a way to enhance your art and make it more exciting to collect - in a sense, we can add light story-telling. For instance, above, we could have just used numbers for the speeds:
Speed: 1, 5, 10
We chose to use words:
Speed: slow, medium, fast
But we can add excitement:
Speed: sluggish, perfect, ultrafast
Or use metaphors or comparisons:
Speed: Turtle, Horse, Jaguar
Now, collectors want to be Jaguar fast!
Have a look through Dr Abstract's Creations to see feature examples. You can easily see the features and their values by expanding the FILTERS icon at top left of the listing. Below is a example from Alien Clover but please look at others to get some ideas. We also tell collectors how many different options there will be. This helps keep track of what features may still have options to reveal.
CONCLUSION
Of course, we can handle rarity with custom code but it is nice to have the ZIM rarity() function with its payload option. It makes creating for (fx)hash easier and gives us focus on handling rarity. ZIM is great for making interactive and generative art NFTs. Please have a read over this Invite to all Generative Artists to explore ZIM.
All the best,
Dr Abstract