Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: random uniform variable with unequal probabilities

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: random uniform variable with unequal probabilities Date Wed, 21 Nov 2012 16:12:47 +0000

```A footnote to this for newish Stata users is to emphasise that
references to -uniform()- here are not typos. Paul is using the old
name for -runiform()-, which still works but is not (prominently)
documented.

Nick

. help uniform()

does throw up

uniform() is an out-of-date function as of Stata 10.1.  It has been
replaced with the runiform() function.

Mata functions uniform() and uniformseed() are out of date as of Stata
10.1.  They have been replaced with mata runiform() and mata rseed().

On Wed, Nov 21, 2012 at 4:04 PM, Seed, Paul <paul.seed@kcl.ac.uk> wrote:
> Here's a solution that does not involve matrices or add-on commands:
>
> *************************** Example code ***************************
> clear
> set obs 200
>
> gen double chosen = int(uniform()*10)+1
>
> forval i = 1/10 {
>         gen double x`i' = int(uniform()*10) +1
>         gen change`i' = uniform() < 1/9
>         replace x`i' = chosen if change`i'
> }
> *************************** End example code ***************************
>
> Date: Tue, 20 Nov 2012 10:36:55 +0000
> From: Nick Cox <njcoxstata@gmail.com>
> Subject: Re: st: random uniform variable with unequal probabilities
>
> On the last point: The crunch is that -rdiscrete()- requires a column
> vector of probabilities. The implication, which I didn't spell out, is
> that you would need an extra loop to get what is being asked for.
>
> Nick
>
> On Tue, Nov 20, 2012 at 10:25 AM, Seed, Paul <paul.seed@kcl.ac.uk> wrote:
>> On Mon, Nov 19, 2012 at 11:44 AM, Tomáš Houška <xbender@gmail.com> wrote:
>>
>>> I need to generate a random variable from uniform distribution, but
>>> the probabilities are not not same for each option. The variable is
>>> discrete in interval [10,19] and is generated 10times. Each time one
>>> of the options has twice as high probability of occurring as the
>>> others. So in some sense, the draw is two-staged - in the first stage
>>> a random number (n) between 1 and 10 is generated and then that
>>> particular n-th option in the interval [10,19] has a probability 0.2
>>> of occurring, the other options have a probability 0.8/9 each.
>>>
>>> How can I generate such variable?
>>
>> Actually, this _is_ a uniform distribution: It can be shown that
>> Pr(X=x) = 0.1  for all integer x in the interval [10,19], = 0 otherwise.
>> So the double negative ("not not same") does for once equal a positive.
>>
>> However,  the second sentence implies that what is produced is not a
>> number, but a group of 10 numbers, and the 10^10 possible groups are
>> definitely not all equally likely.  I expect that Nick's code can be
>> easily adapted to give blocks of 10 numbers.
>>
>> BW
>>
>> Paul T Seed, Senior Lecturer in Medical Statistics,
>> Division of Women's Health, King's College London
>> Women's Health Academic Centre, King's Health Partners
>> (+44) (0) 20
>
> Paul T Seed, Senior Lecturer in Medical Statistics,
>
> Division of Women's Health, King's College London
> Women's Health Academic Centre, King's Health Partners
> 020 7188 3642,
>  paul.seed@kcl.ac.uk,
>
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

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