Disabling objects for render based on their ID/Pass Index

Hi guys,

I’d like to know what would be the best way to disable objects for render (and viewport if possible) based on their ID/pass index.

What I’m doing for now, is using shaders (I get different ones for different parts of the object) with alpha. I’m using a driver that gives me a random number each frames and compare this to the object data. It works perfectly but the only downside using this is that I need at least 96 transparency bounces in the light paths for cycles and that makes my renders take way too much time (5 mins 11 seconds compared to 32 seconds with the objects disabled).

So I was wondering if there was an easy way of reusing my number sequence (IDs) to disabling the corresponding objects instead of making them transparent.

Thanks and have a good day.