Nodes/ComfyUI String Tools/StringToolsBalancedChoiceList
ComfyUI Node

StringToolsBalancedChoiceList

Weighted picks on a list, with hand-rolled weights if you want them

By Taremin·Created 3 years ago·Updated 2 months ago· 3
StringToolsBalancedChoiceList
    • STRING
    seed0
    text_list
    weight_list

    The weighted picker that takes a list - and your own weights

    StringToolsRandomChoiceList picks uniformly from a list of strings. StringToolsBalancedChoiceList is the weighted sibling: same seed + text_list shape, but it can bias the pick in two ways - automatically by counting nested choice nodes, or explicitly through a weight_list input you wire in yourself. That last bit is what makes it worth knowing about even if the auto-balancing never matters to you.

    How it works

    Inputs: seed (INT, default 0) and text_list (a STRING list, force-input) are required; weight_list (an INT list, force-input) is optional. It's an INPUT_IS_LIST node, so text_list arrives as a whole list - typically bundled by StringToolsStringsToList or another list-of-strings producer.

    The weighting logic, in order of what it prefers:

    1. If you supply weight_list and its length matches the list, it uses your weights directly. That's the straightforward way to make a 3-way list pick like 50/30/20 - no graph tricks, just numbers.
    2. Otherwise it auto-balances: if the list came from a branch that contains nested StringToolsRandomChoice/StringToolsBalancedChoice/List nodes, it counts the leaves through that branch and splits the weight evenly across the list items - the same leaf-counting philosophy as StringToolsBalancedChoice.
    3. Neither? Then it's uniform - effectively StringToolsRandomChoiceList.

    Either way, the pick is seeded with your seed, so same seed, same list, same weights, same result.

    Like the non-list balanced node, it keeps a per-run tally of how often each element was picked, and it can print each element's weight and selection count to the console. Note that the handy Debug toggle in ComfyUI's settings is registered for StringToolsBalancedChoice proper; the List variant's debug print lives behind its own debug flag, so you'll mostly use the non-list debug toggle or trust the math. Either way, verifying your hand-rolled weights actually land where you expect is worth doing once.

    The inputs that matter

    • seed (INT, default 0) - change it to re-roll. A StringToolsSeed wired in keeps the number visible.
    • text_list (STRING list) - the candidates. Must be a list, not individual wires.
    • weight_list (INT list, optional) - your explicit weights, one per element, in the same order. Length must match text_list or it's ignored.

    Output is a single STRING - the weighted pick.

    Installing it

    One-pack install. ComfyUI Manager: "Install Custom Nodes", search ComfyUI String Tools (repo Taremin/comfyui-string-tools), install, restart. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Taremin/comfyui-string-tools
    

    Restart fully. No pip dependencies, no model files, MIT license, release 0.0.7.

    Gotchas

    • A weight_list with the wrong length is silently ignored - you fall back to auto-balancing or uniform, and there's no error to tell you. Check the Debug output if your distribution looks wrong.
    • weight_list is INT only. If your ideal weights are 0.5/0.3/0.2, scale them to whole numbers (5/3/2) - the code normalizes via random.choices, which works fine on raw counts.
    • Lists and single strings still don't mix: text_list wants a list port. Bundle with StringToolsStringsToList first.
    • An empty list returns an empty string. Half-disconnected upstream bundles are the usual culprit.
    Categorystring-tools

    Inputs (3)

    NameTypeDefaultDescription
    seedINT00–18446744073709550000
    text_listSTRING
    weight_listoptINT

    Outputs (1)

    NameTypeDescription
    STRINGSTRING