Also, the one that she slept with was next in line after the last brother was constrained from giving her first husband an heir.Note that Tamar was not actually a prostitute.
She only submitted to Judah. She might have dressed as a prostitute, but in reality she offered her body to one man only, and then kept herself solely for that man. The whole prostitute thing was a deliberate disguise. She was not actually a prostitute and never engaged in prostitution, as she never slept with multiple men and never accepted payment.
He was not a rando.