Bookmark and Share

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, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Generate a random double between -1 and 1

From   Aaron Kirkman <>
Subject   st: Generate a random double between -1 and 1
Date   Sun, 2 Sep 2012 14:26:12 -0500

I have a situation where I need to generate random real numbers
between -1 and 1, and I'm wondering if there is a better way to
accomplish this. Right now, I use this:

set obs 100000000
gen double eps = -1 + 2 * runiform()
summ eps

While this is probably acceptable, as the theoretical mean is 0, it's
problematic because multiplying 2 * runiform() excludes half the
possible values between -1 and 1. Is there a built-in way to do this
in a way that won't exclude values, or is this method sufficient? In
practice, I may generate as few as 30 random numbers and as many as
100,000,000 (if that's relevant).

Thank you,
Aaron Kirkman
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index