Tiny trick: use the Switch node (Expression mode) as a random router

Just stumbled on something simple that might save someone a community node install: you can turn the native Switch node into a random router without any code.

How:

  1. Add a Switch node, set Mode to Expression
  2. Set Number of Outputs to however many branches you want (N)
  3. Set Output Index to:
{{ Math.floor(Math.random() * N) }}

(replace N with your actual number of outputs)

That’s it — each execution randomly lands on one of the N outputs. Math.random() returns 0–1, so Math.floor(x * N) maps cleanly onto index 0 to N-1.

Figured I’d share since I’ve seen a few community nodes built specifically to solve this (random/percentage routing), but the built-in Switch node already handles the basic case out of the box.

If you try it out, let me know if it helps or if you run into any edge cases — curious to hear how others end up using it.

Hi there,

Great find, this is indeed possible like this and no community node needed.

However, I am wondering what the practical usecase for this would be. Except for maybe debugging this doesn’t sound very useful to me.

1 Like

Thanks for the comment! Honestly I didn’t have a specific problem in mind — I was just poking at “how could I easily pick a path at random in n8n” and this fell out of it. I believe that, like you said, it could be useful for testing/debugging or maybe load distribution.

Curious to see if anyone can think of something else.

1 Like