It is completely random.
The RNG just gives a random opponent to you.
The only fixed ones are Trainer Red at 20 wins for singles, Trainer Blue at 20 wins for doubles.
Also, every 10 wins, you face a special Trainer in all formats (like Wally, Kiawe, Cynthia). These trainers are tough, and have properly trained Pokemon, unlike the others who usually aren't very powerful and can be beaten easily. They also use Mega Evolutions.
More info online.
Hope I helped!
Source: I have analysed everything in my past one year of battling. Faced the same trainers sometimes, but completely random.