probability

I got this code form a book:
1
2
3
4
5
6
7
8
9
10
long double probilaty( unsigned numbers, unsigned picks)
{
	long double result = 1.0;
	long double n;
	unsigned p;

	for(n = numbers; p = picks; p > 0; n--, p --)
		result = result * n / p;
	return result;
}
And I was wondering how this predicts probability.
EDIT: I change void to long double.
Last edited on
for(n = numbers; p = picks; p > 0; n--, p --)

I don't think that's valid...semicolons are used to seperate for loop things, so this loop would run as long a p = picks (which is true as long as picks != 0).
I think there must have been a transcription error and OP meant a comma after numbers.
That being the case, the function returns

1 - n! / p!

Recall from basic probability theory that n! / p! is the number of ways that P items can be chosen from a pool of N elements without replacement.

That said, the function doesn't make sense to me, because picks <= numbers, in which case n! / p! >= 1, which means it is returning the number of ways P items can be chosen from N elements, which is NOT a probability, but COULD BE USED in a probability calculation.


Topic archived. No new replies allowed.