How can I get canonical transformation from this kind of generating function? My question is about how to get canonical transformation $$\varphi(q,p) = (Q(q,p),P(q,p))$$ whose generating function is $$W(Q,p) = Q^2 \sin(p).$$
I know that $W(Q,p)$ is type-3 generating function (in https://en.wikipedia.org/wiki/Canonical_transformation#Type_3_generating_function), so

$$q = - \frac{\partial W}{\partial p} = -Q^2 \cos (p), \qquad P = - \frac{\partial W}{\partial Q} = -2Q \sin(p)$$

holds. So it is easy to get

$$Q(q,p) = \pm \sqrt{\frac{-q}{\cos (p)}}, \qquad P(q,p) = \mp 2 \sin(p) \sqrt{\frac{-q}{\cos (p)}}.$$

However, the freedom of sign on the outside and inside of root symbol of $Q(q,p)$ and $P(q,p)$ left.
How can I determine sign on the outside of root symbol uniquely?
And how can I determine the range of $(q,p)$ that makes $\frac{-q}{\cos (p)}$ is positive?
It is bit weird that there is some range that canonical transformation based on $W(Q
,p)$ is not well-defined.
For example, when $(q,p) = (1,0)$, than $\frac{-q}{\cos (p)}  = \frac{-1}{1} < 0$ holds.
Is $W(Q,p)$ not a "proper" generating function?
 A: There is no natural choice of the square root. It could be possible if your canonical transformation is part of a flow and that starts at a well defined transformation (say the identity). Note that it would work as long as you don’t cross $0$.
$-\frac{q}{\cos p}>0$ can be solved by looking at the possible cases $q>0,q<0$ since it’s a product. By seeing that $\cos p>0$ (resp. $\cos p<0$) iff $p\in (-\frac{\pi}{2}, \frac{\pi}{2})+2\pi\mathbb Z$ (resp. $p\in (\frac{\pi}{2}, \frac{3\pi}{2})+2\pi\mathbb Z$) so you get the domain (alternating half-plane stripes):
$$
(q,p)\in \left[\mathbb R_+^* \times \left((\frac{\pi}{2}, \frac{3\pi}{2})+2\pi\mathbb Z\right)\right]\cup \left[\mathbb R_-^* \times \left((-\frac{\pi}{2}, \frac{\pi}{2})+2\pi\mathbb Z\right)\right]
$$
There is no reason for the generating function to define the canonical transformation globally. In particular a type $3$ only works when $Q,p$ are independent, but this may not always be the case. Generally, the existence of a generating function is guaranteed locally.
Hope this helps.
