Skip to content

Instantly share code, notes, and snippets.

@wuyongzheng
Created October 23, 2012 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuyongzheng/3937689 to your computer and use it in GitHub Desktop.
Save wuyongzheng/3937689 to your computer and use it in GitHub Desktop.
Generate 3 uniform random variables that sum to 0
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