I have no understanding of the science behind drugs testing, so my question could be completely stupid, but…
Yes it is complicated so perhaps worth doing some research. Maybe reading one of the many books buy a doper. From those I’d say that people expected be tested during the race so either they cheated by doping out of the race or doping during the race in a way that they believed wouldn’t be detected.
Removing the random element would be a mistake IMHO. If you were doping and knew that keeping out of the top 3 meant no testing then that is a loop hole to exploit. Lots of crucial Tour stages a GC rider can move up the places but not place in the top 3 in a stage.
But I’d say the key is out of competition testing. Making sure that people get tested at the time and day of the authorities choosing.
My speculation would be could we increase the “the duty of care” on teams to make sure their riders are clean. In the first Robert Miller book the surprise to me is how little his team “cared” for him. He was treated like a consultant not an employee