Skip to content

Instantly share code, notes, and snippets.

@alphazwest
Created January 10, 2022 00:02
Show Gist options
  • Save alphazwest/ebb6edfaa674d38ceab1ab9f23bf4e68 to your computer and use it in GitHub Desktop.
Save alphazwest/ebb6edfaa674d38ceab1ab9f23bf4e68 to your computer and use it in GitHub Desktop.
A poorly conceived approach for generating randomized attribute combinations given some initial values and weights.
"""
Sources like OpenSea will pull metadata from a contract URI
specified in a e.g. tokenURI function within a contract.
Sample Reference: https://docs.opensea.io/docs/metadata-standards
OpenSea particularly will use values stored within the "attributes"
key, found within a JSON-formatted, to display verifiably-rare
characteristics of an NFT.
Notes:
This illustrates poor attempts at randomness
"""
# Below are definitions of various example traits with example values
import json
import random
import sys
# Below are the definitions of various
archetype = ['human', 'creature', 'robot']
archetype_weights = [5, 25, 70]
epidermis = ['gold', 'silver', 'steel']
epidermis_weights = [5, 25, 70]
outer_clothing = ['labcoat', 'sportcoat', 'blouse']
outer_clothing_weights = [5, 25, 70]
undershirt = ['none', 'turtleneck', 'v_neck']
undershirt_weights = [5, 25, 70]
ppe = ['safey_goggles', 'gloves', 'respirator']
ppe_weights = [5, 25, 70]
# Below is an example combination
results = []
def generate() -> dict:
"""
A function to generate random attribute combinations
"""
# define an attribute container
image = {
'archetype': random.choices(archetype, archetype_weights)[0],
'epidermis': random.choices(epidermis, epidermis_weights)[0],
'outer_clothing': random.choices(outer_clothing, outer_clothing_weights)[0],
'undershirt': random.choices(undershirt, undershirt_weights)[0],
'ppe': random.choices(ppe, ppe_weights)[0]
}
# Generate randomized values
if image in results:
return generate()
return image
if __name__ == "__main__":
# Generates the attributes
for i in range(15):
# Generate an image
image = generate()
# generate a hash for ID
_id = hash(
image['archetype'] +
image['epidermis'] +
image['outer_clothing'] +
image['undershirt'] +
image['ppe']
) & sys.maxsize
image["id"] = _id
# stream the output to console as a
# json-formatted string object
print(json.dumps(image))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment