'hits' and 'n' are both integers and hits < n, therefore hits/n =3D 0
in integer arithmetic.
You need to convert at least one of them to a floating point type,
e.g., with 'cast(double)(hits)/n'.
On 10 November 2011 21:08, Fabian <talk2fab online.de> wrote:

Hey guys,
I've got a problem - I've just written a few lines to approximate PI with=

This is not related to your question. I just wanted to point out that it
is better to use the normal increment operator in the majority of cases.
(I avoid calling it pre-increment, because that name suggests a more
complicated semantics than there actually is.)
On 11/10/2011 12:08 PM, Fabian wrote:

for(int i = 0; i<= n -1; i++)

hits++;

In both of those lines you want to merely increment the value of a
variable. The post-increment operator that you have used has a different
meaning: "increment this variable but use its previous value in this
expression."
Since the previous value is ignored in those expressions, it is better
to just increment: ++i and ++hist.
Semantically cleaner... :)
Ali

++i is unidiomatic, and if the result is unused it means the same thing
as i++. So, I'd actually go with i++. The only reason why one would use
++i is because it is less efficient for C++ iterators, but D does not
have that problem at all. This is really not important though.
however, about:
for(int i = 0; i <= n -1; i++){}
this is the way to go (less gotchas if index is a size_t, which it
sometimes should)
for(int i = 0; i < n; i++){}
But because this exact construct is so common in C and C++, D has an own
syntax for it:
foreach(i;0..n){}
This does exactly the same thing.
Always use foreach for trivial iteration.

size_t can easily bite you* in the ass:
import std.algorithm;
void main()
{
int[] a;
int val = max(0, a.length - 1);
assert(val > 0); // NG, woops!
}
* = me
That one is easy to catch, but in a complex expression you might have
ints and size_t and everything gets converted to unsigned, and then
you end up with some value over int.max which is implicitly assigned
to an int. :/