ComfyUI Node

Boolean Random

A real coin flip you can wire into your graph

By ArtemKo7v·Created 25 days ago·Updated a day ago· 0
Boolean Random
    • current
    • saved
    last_valuefalse

    ComfyUI randomizes numbers for you all day - every seed widget has randomize in its dropdown. What it doesn't have is a coin. Boolean Random gives you one true/false per run that you can wire into anything with a boolean input, which is what you need the moment you want a branch to be random rather than a value.

    What it is, and why you'd want a coin

    The honest summary: it flips a coin, and it remembers what the coin said last time. Two outputs, no widgets you can type into, and one behaviour that matters far more than the flip itself - it re-runs on every queue, and it drags everything downstream with it. That's the piece people end up hand-rolling. In a thread about automating a workflow where a switch sent output to path A or path B depending on a random boolean, the diagnosis was ComfyUI's backwards-propagation solver: it only re-executes what looks changed, so a switch that hasn't changed doesn't pull a new branch behind it. The suggested workaround was "stick a node at the front which always refreshes (say a random number node) - it's a kludge." This node is that node, except it emits a boolean and it's built to fire every time.

    So: random A/B prompts, random "should this second pass run", random choice between two save paths, random toggle for an optional effect. If you want variety per queue rather than per generation, that's what an always-rerunning branch selector gives you.

    How it works

    The flip is bool(secrets.randbelow(2)) - the OS cryptographic random source, not ComfyUI's seeded generator. Two consequences worth internalizing: it's an honest 50/50 with no modulo bias, and you cannot reproduce a run by reusing a seed, because no seed is involved. There's no probability knob either; it's half or nothing.

    The save half is the interesting design. last_value is a required input, but it's not a setting you fill in - the frontend disables the widget. When the node executes it hands back the previous contents of last_value as the saved output, and writes the freshly flipped value into last_value via a UI message. That widget is part of the node, so it gets saved into your workflow file: reopen the workflow tomorrow and the node still knows what the coin said last time.

    Inputs and outputs that matter

    • last_value (BOOLEAN, default false) - read-only. On a brand new node it starts false; after each run it holds the coin's last result.
    • current (BOOLEAN) - this run's flip. This is the one you wire.
    • saved (BOOLEAN) - the value from before this run. Use it to compare consecutive runs ("did it land differently this time?"), or just ignore it.

    Wiring it: BOOLEAN outputs connect to BOOLEAN inputs. On the receiving node, right-click a checkbox or boolean widget and use Convert widget to input, then plug current in. The common destinations are switch nodes from utility packs - Impact Pack ships a family of them under utilities - and any node in your graph that gates an optional pass behind a boolean.

    Installing it

    Manager, search ComfyUI Useful Stuff Nodes, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/ArtemKo7v/ComfyUI-UsefulStuffNodes
    # restart ComfyUI, then reload the browser page
    

    Two shared details for the whole pack: no dependencies at all (empty requirements.txt, standard library only, no models), and the nodes show up under the ArtemKo7v category rather than anything obvious. The browser reload is not optional here - the read-only last_value widget and its live update come from a frontend script, web/js/stored_value.js. Skip it and you'll see an editable box that never updates.

    Common issues

    The last_value box won't let you type. Correct - it's written by the node, not by you. It only looks editable if the frontend extension didn't load; hard-refresh the page.

    Everything after it re-runs every queue. The node marks itself always-changed on purpose. That's the feature, but it means a workflow with a random boolean in it never gets the benefit of ComfyUI's cache, and the slowdown behind it is real. Put it upstream of as little as possible.

    New branch, same image. Your flip did happen, but a node downstream that only depends on an unchanged input stayed cached - pair the coin with an actual switch node, and remember that a branch you never route into won't refresh for you.

    Debugging with it in the graph is miserable. Randomizing while you change other things is how you spend an evening chasing ghosts. Set the branch statically, fix the workflow, then put the coin back in.

    CategoryArtemKo7v

    Inputs (1)

    NameTypeDefaultDescription
    last_valueBOOLEANfalse

    Outputs (2)

    NameTypeDescription
    currentBOOLEAN
    savedBOOLEAN