The coolname library treats the configuration space as a virtual N-dimensional array.
- Calculate Combinations: It calculates the total number of possible combinations based on the configuration (e.g., using Cartesian products or nested lists).
- Generate Index: When calling
RandomGenerator.generate_slug, the library generates a single random integer using randrange(N), where N is the total number of possible combinations. - Map to Element: This integer is then used as an index to pick a specific element from the N-dimensional combination space.
Example Calculation:
Given a config with:
price: 2 itemscolor: 3 itemsobject: 11 items (5 from footwear + 6 from hat)
The total combinations are $2 \times 3 \times (5 + 6) = 66$. A call to randrange(66) will map to one of these 66 unique slugs.
import coolname
config = {
'all': {
'type': 'cartesian',
'lists': ['price', 'color', 'object']
},
'price': {'type': 'words', 'words': ['cheap', 'expensive']},
'color': {'type': 'words', 'words': ['black', 'white', 'red']},
'object': {'type': 'nested', 'lists': ['footwear', 'hat']},
'footwear': {'type': 'words', 'words': ['shoes', 'boots', 'sandals', 'sneakers', 'socks']},
'hat': {'type': 'phrases', 'phrases': ['top hat', 'fedora', 'beret', 'cricket cap', 'panama', 'sombrero']}
}
generator = coolname.RandomGenerator(config)