Generalized Donsker-Kac formula for dominant modes in waveguides with complicated geometries and constitutive properties