--fast #290
--fast
#290
Replies: 2 comments
|
Toy Example: Usual Method ( #include <stdio.h>
#include <stdlib.h>
int main() {
int sum = 0;
for(int i = 0; i != 1000000000; i++){
sum += (rand() % 6) + 1;
}
return 0;
}Box-Muller Method ( #include <stdio.h>
#include <stdlib.h>
#include <math.h>
double random_normal(double mean, double std) { /* Box-Muller. */
static double cached = 0.0;
double x, y, r, res;
if (cached == 0.0) {
do {
x = 2.0 * rand() / RAND_MAX - 1;
y = 2.0 * rand() / RAND_MAX - 1;
r = x * x + y * y;
} while (r == 0.0 || r > 1.0);
double d = sqrt(-2.0 * log(r) / r);
double n1 = x * d;
double n2 = y * d;
res = n1 * std + mean;
cached = n2;
}
else {
res = cached * std + mean;
cached = 0.0;
}
return res;
}
int main() {
int total = 1000000000;
float die = 6;
float mean = (die+1)/2;
int result = random_normal(mean, 1);
return 0;
}
TODO: Check |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
https://mathworld.wolfram.com/Dice.html
Sacrifice accuracy for speed by sampling on a normal distribution between Mac and min of the combination of dice
All reactions