Nodes/ComfyUI-0246/Random Int
ComfyUI Node

Random Int

Randomness that actually works inside a Loop

By Trung0246·Created 3 years ago·Updated about a year ago· 137
Random Int
    • rand_int
    valrand,0
    min0
    max9007199254740991
    batch_size2
    mode
    seed0

    Random Int is the 0246 pack's answer to the "random number" node, and the one difference it cares about is written right in the pack's own docs: it generates numbers server-side to work with Loop. If you've ever wired a normal random-int node into an iteration workflow and watched every step consume the same value, you know exactly why this matters.

    The usual random node rolls the dice on the client - the number is baked into the prompt when you hit Queue. Inside the 0246 Loop, which re-executes chunks of your graph mid-run by messing with ComfyUI's execution internals, that client-side value is static: iteration one and iteration twenty get the same seed. Random Int keeps a server-side random.Random() instance per node, seeded from your seed input, and asks it for fresh values as the loop actually executes. Loop 20 different seeds through one KSampler? That's the intended use, and the README's demo is exactly that.

    How it works

    The node keeps a small database (RANDOM_DB) keyed by node id and the current prompt run, so state is scoped to one queue and doesn't leak between runs. The val input is a comma-separated list that tells it what to do per slot:

    • rand - roll a new random int between min and max
    • a literal number - use that exact value
    • add / sub - step the previous value up or down by one

    batch_size (default 2) is how many ints it produces. The mode enum is the other knob: usual rolls fresh each run, keep holds the values across executions so your loop doesn't reshuffle mid-flight, and force regenerates even when the seed hasn't changed. It returns rand_int, a list of INT values - one per batch slot - which is exactly the shape Loop and Hold want to consume.

    The inputs that matter

    • val - default "rand,0": one rand plus a fixed 0. This is the field you'll actually edit.
    • min / max - the range. Setting min above max raises an error ("Min is greater than max."), which is a nice touch.
    • batch_size - how many values per execution.
    • mode - usual / keep / force.
    • seed (optional) - the server-side random.Random() seed. Same seed, same sequence, as long as you don't change mode to force.

    How to install it

    Same story as the whole pack: ComfyUI Manager, search "ComfyUI-0246", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Trung0246/ComfyUI-0246
    

    then restart. No models to download; dependencies are wrapt, natsort, and wat-inspector, handled by the pack's requirements.txt.

    Common issues & troubleshooting

    Values don't change between runs. You're probably in keep mode, or the loop isn't re-invoking the node. keep is for holding values across executions, not for rolling fresh ones.

    Every iteration gets the same number even with a seed set. That's the normal loop behavior - the server instance re-seeds only when the seed input changes or you force it. Change the seed per iteration (another Random Int feeding it, or a Hold) and it walks the sequence.

    The node "always runs." Intentionally. When val contains rand, add, or sub, its IS_CHANGED returns NaN, so ComfyUI re-executes it every prompt - that's what makes the server-side state update at all. If you see it firing when you expected caching, that's the feature working, not a bug.

    Category0246

    Inputs (6)

    NameTypeDefaultDescription
    valSTRINGrand,0
    minINT0-9007199254740991–9007199254740991
    maxINT9007199254740991-9007199254740991–9007199254740991
    batch_sizeINT21–9223372036854776000
    modeCOMBO3 options: usual, keep, force
    seedoptINT0-1125899906842624–1125899906842624

    Outputs (1)

    NameTypeDescription
    rand_intINT