use pwgen instead of custom function

Technically you don't need the num_pw argument if this is running as a script.