# Re: st: Generate a random variable with a range constraint

 From Antoine Terracol To statalist@hsphsun2.harvard.edu Subject Re: st: Generate a random variable with a range constraint Date Sat, 06 Dec 2008 20:04:50 +0100

```Hello David,

I'm not sure this is what you want, but the following code

clear
set obs 5000
scalar sig=70
scalar mu=0.15
scalar up=95
scalar low=45
```
g /// wt=sig*invnorm((normal((up-mu)/sig)-normal((low-mu)/sig))*runiform()+normal((low-mu)/sig))+mu
```su wt

```
generates a random variable tuncated at 'low' and 'up', and whose *untruncated* distribution is normal with mean 'mu' and standard deviation 'sig'
```
hope this helps,

Antoine

David Collins wrote:
```
I apologise for what may be a fairly rudimentary question. I want to generate a random variable (wt), n = 5000; as follows;
```
set obs 5000
set seed 1000
g wt = 70 * rnormal(1, 0.15)

```
However, wt also needs to be constrained as follows, 45 < wt < 95. I am having difficulty incorporating the constraint into the generate function, and would appreciate any help.
```
--David

```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```