Write a programExercise 4/4Intermediate30 XP

Estimating π by chance

Task

Write a function odhad_pi(pocet) that estimates π with the Monte Carlo method: it generates pocet random points (x, y), where x and y are random decimal numbers from 0 to 1 (the function random.random()), and counts how many fall into the quarter circle, that is x² + y² <= 1. It returns 4 * (points in the quarter circle) / pocet.

The more points, the more accurate the estimate - with 20,000 points it comes out at about 3.14.

Examples

  • Call

    print(abs(odhad_pi(20000) - 3.14159) < 0.1)

    Expected output

    True

After you submit, 4 hidden tests will also check your solution: returns a decimal number, one point gives 0 or 4, the result depends on chance, the estimate is between 0 and 4.

Python starts the first time you run code.

Ctrl+Enter runs the program. Tab indents. Press Esc, then Tab, to leave the editor.

Input (stdin)

Each line = one call to input().

Output

Hints

Try it yourself first. Each hint reveals a little more - and lowers the reward a little.

Signed out, hints are free - but no XP is awarded either.

Solution

The solution unlocks after the last hint.