Need help on Gibbs sampling with truncated normal and gamma I am trying to use Gibbs Sampling to simulate a random sample from a joint distribution $f(\beta ,{{Z}_{1}},...,{{Z}_{75}},{{\lambda }_{1}},...,{{\lambda }_{75}})$, where the fully conditioned distribution function are:
$$\beta |{{Z}_{1}},...,{{Z}_{75}},{{\lambda }_{1}},...,{{\lambda }_{75}}\sim N\left( \frac{\sum\limits_{i=1}^{75}{{{\lambda }_{i}}{{Z}_{i}}}}{\sum\limits_{i=1}^{75}{{{\lambda }_{i}}}},\frac{1}{\sum\limits_{i=1}^{75}{{{\lambda }_{i}}}} \right)$$
For i=1,...,24,
${{Z}_{i}}|\beta ,{{\lambda }_{1}},...,{{\lambda }_{75}}$ ~ left truncated normal at 0
$${{f}_{L}}\left( t;\beta ,0,\frac{1}{\lambda } \right)=\left\{ \begin{array}{*{35}{l}}
   0 & \text{if  t}\le 0  \\
   \frac{{{e}^{-\frac{{{\lambda }_{i}}}{2}{{(t-\beta )}^{2}}}}}{\sqrt{2\pi /{{\lambda }_{i}}}[1-\Phi (-\beta \sqrt{{{\lambda }_{i}}})]} & \text{if t}>0  \\
\end{array} \right.$$
$$$$
For i=25,...,75,
${{Z}_{i}}|\beta ,{{\lambda }_{1}},...,{{\lambda }_{75}}  \sim$ right truncated normal at 0
$${{f}_{R}}\left( t;\beta ,0,\frac{1}{\lambda } \right)=\left\{ \begin{array}{*{35}{l}}
   \frac{{{e}^{-\frac{{{\lambda }_{i}}}{2}{{(t-\beta )}^{2}}}}}{\sqrt{2\pi /{{\lambda }_{i}}}[\Phi (-\beta \sqrt{{{\lambda }_{i}}})]} & \text{if t}\le 0  \\
   0 & \text{if  t}>0  \\
\end{array} \right.$$
$$$$
For i = 1,...,75,
$${{\lambda }_{i}}|\beta ,{{Z}_{1}},...,{{Z}_{75}}\sim {\rm Gamma}\left( \frac{5}{2},\frac{2}{4+{{({{Z}_{i}}+\beta )}^{2}}} \right).$$
I am having trouble implementing this.
My algorithm: choose some fixed initial $Z_{i}$'s and $\beta$, and generate $\lambda_{i}$ from Gamma distribution.
Let $Z_{i}=0$ and $\beta=0$, I get $\lambda_{i} \sim {\rm Gamma}(5/2, 2/4)$.  I then use these $\lambda_{i}$'s to generate $Z_{i}$. 
Next step: how do I generate truncated normal, how do I know if my t is greater than zero or not?  Any experts would like to do what I am doing right or wrong?  Maybe suggestions?  All welcome!
Thank you very much in advance, I have been very confused about this!
Note: I am using Java.
 A: This should probably be a comment, but getting nicely formatted TeX in the comment field drives me nuts.
If your problem is generating a truncated normal distribution in java you have a number of choices.


*

*Easiest, if your interval has high enough probability is just to generate normal random variables until one falls into the range.  This is the essence of guest's selection.

*Use accept-reject with some easy to simulate distribution, say a uniform over the interval in question, or an exponential if you are dealing with a tail.

*Use the fact that the distribution function for the truncated normal is just a linear function of the distribution function for the normal.  If $\Phi(x)$ is the distribution function of the normal distribution and $\phi(x)$ is the density, and you are truncating so that $a\le x \le b$ then the density function is
$$\phi_{[a,b]}(x) = \frac{\phi(x)}{\Phi(b)-\Phi(a)}$$
and 
$$\Phi_{[a,b]}(x) = \frac{\Phi(x)-\Phi(a)}{\Phi(b)-\Phi(a)}.$$
both for $a\le x\le b$. 
So if we want to simulate a truncated normal random variable, we choose a random $[0,1]$ uniform random variable $u$ and compute
$$\Phi^{-1}(\Phi(A)+u(\Phi(b)-\Phi(a)) $$
This is what @joint_p was doing.  In R $\Phi(x)$ is pnorm(x) and $\Phi^{-1}(x)$ is qnorm(x).  What these are in the java library you are using, I have no idea.
