On 30.08.2015 23:13, Johann Klammer wrote:
> On 08/30/2015 09:43 PM, Prroffessorr Fir Kenobi wrote:
>> this is some benchmark i fond on some blog (called 'null program')
>>
>> #include <stdio.h> #include <stdlib.h>
>>
>> int trial() { int count = 0; double sum = 0; while (sum <= 1.0) { sum
>> += rand() / (double) RAND_MAX; count++; } return count; }
>>
>> double monteCarlo(int n) { int i, total = 0; for (i = 0; i < n; i++)
>> { total += trial(); } return total / (double) n; }
>>
>> int main() { printf("%f\n", monteCarlo(100000000)); return 0; }
>>
>> this measures if i understuud the problem how many random values
>> (form 0. to 1.) to add to get a sum over 1.0 (it could be thinked
>> maybe that it shold be 2 on average but it soes not seem so (or maybe
>> im wrong in understanding it) dont matter) They say that it is e =
>> 2.71828...
>>
>> this is anyway interesting form the optimistation points of view,
>> this kode above on my old C2D executes 7.6 s and yeilds to 2.178154
> Looks closer to 2 than e....
It is e. Fir just doesn't care and smash keyboard without looking;-)
Lets n(x) be an expected value of times one need to draw
a random number idd U[0..1] to gat sum smaller than x.
We are looking for n(1).
We draw a number p. If p>=x we are done. In other case
we need another n(x-p) draws.
n(x) = 1 + \int_0^x n(x-p) dp
I'm sure there is a methodical way to solve the equation,
but we can guess n(x) = exp(x). Ant it works.
1+\int_0^x exp(x-p) dp = 1 - exp(x-p)|_0^x = 1-(exp(0)-exp(x))=exp(x)
n(1) = e.
>> the quest optimise it.. to optimise it i think there should be
>> written a better rand() routine that both would have good
>> probabilistic properties and faster execution..
>>
>> can someone provide such routine?
>>
>
> take the code for rand and the functions it calls from the c library and inline them in this file.
I'm pretty sure a decent compiler do it for us.
A version with default_random_engine (propably x = x * 48271 %
2147483647) from <random> without any trick, is only a bit slower
int trial2() {
static random_device rd;
static default_random_engine gen(rd());
uniform_real_distribution<double> dist(0.0,1.0);
int count = 0;
double sum = 0;
while (sum <= 1.0) {
sum += dist(gen);
count++;
}
return count;
}
double monteCarlo2(int n) {
int i, total = 0;
for (i = 0; i < n; i++) {
total += trial2();
}
return total / (double) n;
}
c like rand 2.718359
5.64892s
c++11 <random> 2.718232
5.73054s
c like rand 2.718111
5.67173s
c++11 <random> 2.718149
5.73041s
c like rand 2.718318
5.67199s
c++11 <random> 2.718329
5.73014s
And mt19937, a real pseudorandom number generator, not a toy,
get results below 8seconds.
bartekltg