Created
October 23, 2012 08:46
-
-
Save wuyongzheng/3937689 to your computer and use it in GitHub Desktop.
Generate 3 uniform random variables that sum to 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See the question in http://stackoverflow.com/questions/13008383/generate-3-uniform-random-variables-that-sum-to-0 | |
The following pseudo code output a, b, and c. Function rand() returns real number uniformly distributed in (0,1). | |
s = rand() * 3; | |
r = rand(); | |
if (s < 1) { | |
a = r; | |
b = 1 - 2*r; | |
c = r - 1; | |
} else if (s < 2) { | |
a = 1 - 2*r; | |
b = r - 1; | |
c = r; | |
} else { | |
a = r - 1; | |
b = r; | |
c = 1 - 2*r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment