I've heard that in Judaism, God don't just randomly appoint people to be a prophet. The prophet must be some "good" guy or pious or whatever.
If that's the case, why is balaam a prophet? He's not very righteous isn't he? Or is he?
Rashi on Numbers 22:5, s.v. "Eretz Benei Amo" ("the land of his people"), says Balaam was a special case:
ואם תאמר מפני מה השרה הקב"ה שכינתו על גוי רשע, כדי שלא יהא פתחון פה לאומות לומר אלו היו לנו נביאים חזרנו למוטב, העמיד להם נביאים והם פרצו גדר העולם, שבתחלה היו גדורים בעריות וזה נתן להם עצה להפקיר עצמן לזנות:
If you ask, “Why did God bestow His Shechinah on a wicked gentile?” [The answer is] so the nations should not have an excuse to say, “Had we had prophets we would have repented.” So He assigned them prophets, but they breached the [morally] accepted barrier, for at first they had refrained from immorality, but he [Balaam] advised them to offer themselves freely for prostitution. — [Mid. Tanchuma Balak 1, Num. Rabbah 20:1]