Code doesn't work

For some reason it is expecting me to put other logical operators instead of sum.

To add 1 to a variable, you should use:

circle_points = circle_points + 1
1 Like

Other question:

random_number generates the same numbers - is there any way to make them random?

By default, the seed of the pseudo-random number generator is the same each time you launch your program (useful for debugging). You must modify that seed:

You can first try CALL RANDOM_SEED (but the result may depend on your OS and your compiler).

Note also that you’d better copy/paste your code in your post instead of attaching a screenshot. In that way, we will be able to copy it and compile it ourselves.
Put your code between triple quotes (with the name of the language besides the first triplet for syntax colouring):

```fortran

and

    ```
2 Likes

And there is an error in your algorithm. The principle is that the ratio \frac{\text{number of points in the disk}}{\text{total number of points in the square (including those in the disk)}} will tend toward the ratio \frac{\text{area of the disk}}{\text{area of the square}} when the number of points tends toward \infty.

So you don’t need the else, which is in fact counting the points outside the disk, but you just have to divide by 101 (the total number of points) in your example.

Another problem is that you have an euclidean division when computing \pi because the two variables are integers. You should write:

pi = (4.0 * cp) / 101

Instead of using the integer circuit of you CPU, the compiler will use the floating point circuit (FPU) because you divide a real by another number. In Fortran, like in C, the two kinds of division are noted the same way, with a slash. The compiler decides.

When your program is OK, you can play to increase the total number of points, by multiplying it by 10 until the computing time becomes too long. Then you can try to use the optimization options of the compiler to improve it.

MonteCarloPi.f90 (386 Bytes)

Is this fine?

1 Like

Actually this is processor dependent. Some compilers always start when the same seed by default, and some start with a random seed (read from a clock or whatever)

Thanks, that was what I remembered with Visual Fortran under Windows. I had written a procedure computing a seed from DATE_AND_TIME().

@vmagnin

Yes, good job.
If you want to study the effect of the number of points, it is better to comment the print (with a !) that is inside the loop (the scrolling in the terminal will impede a lot the performance).

2 Likes

Thanks, mate.

Message me another task in DM’s for practice, I will appreciate that.