#### Archived

This topic is now archived and is closed to further replies.

# Simple(?) Statistics...

This topic is 5572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to write a function that is given a chance of something happening and how many times this test is performed, independently. It should then return a possible number of times that the test succeded, with the correct bias in its result. i.e. if I graphed the results of calling it many times onto a histogram it would produce a binomial distrbiution. I guess that to do this you require an inverse cumulative binomial distribution, but I''m not 100% sure thats the right way to go about doing it, mainly cos that looks nasty. Either way, I don''t know how to do it - someone should know! (And google just lists lots of toolsets that do it, so all that does is tell me its possible!) Another way of describing the problem is with the psuedo code:
int n = 0;
for (i=0;i<number of tests;i++) if (test passed for one individual event) n++;
return n;

This would work, but I would prefere it in O(1) time, or possibly O(log something), but certainly not O(n) like the above code, as I could be calling it with 100000+ tests quite easily, and expecting to have time left to do other things in real time. Thanks for any help in advance, Lethe

##### Share on other sites
You are correct. That is how it is done. For example, a monte carlo generation of pi:

srand(time(0));
unsigned int hit = 0;
unsigned int miss = 0;
while(!kbhit())
{
double x, y;
x = MAX_RAND/rand();//may need to cast as doubles
y = MAX_RAND/rand();
if((x*x+y*y) <=1)
{
++hit;
}
else ++miss;
}
// ratio will be pi. because:
// area_of_unit_circle = pi*rr
// area_of_unit_square = (2r)(2r) = 4rr
// r = 1 since this is unit circle and square.
double pi = 4.0*static_cast(hit)/static_cast(miss); //er I think. maybe flip the numerator and denominator..

depending on what your function to do your fancy binomial distrobution is, you will put it in for where i did my calculation of pi.

If you can''t be bothered to do all this, you might look into SPSS which has histographical graphing functions and a scripting language which is pretty easy to pick up.

##### Share on other sites
quote:
Original post by Lethe
I am trying to write a function that is given a chance of something happening and how many times this test is performed, independently. It should then return a possible number of times that the test succeded, with the correct bias in its result. i.e. if I graphed the results of calling it many times onto a histogram it would produce a binomial distrbiution.

So, you''re saying you want to get a different value each time, centred around (probability * number of tests), yeah?

Personally I think the Boost library''s Normal distribution should do the trick, as long as you pass it the right parameters. No idea what its complexity is, however.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 9
• 9
• 13
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007284

×