Reputation: 43
I am trying to plot the Monte Carlo estimation of Pi in Geogebra.
I created a circle with radius 1, centered at the origin, and it is inscribed in a square.
To generate a sequence of n random points (determined by a slider), I added the following code in the input bar:
Sequence((RandomBetween(-1,1), RandomBetween(-1,1), i, 1, n)
The points appearing are only on the coordinates (-1,0), (1, 0), (0,1), (0, -1), and (0,0).
How do I get the points to appear on decimal values as well? For example (0.2, 0.4)?
Upvotes: 0
Views: 125
Reputation: 517
You can use RandomUniform for this, i.e
points = Sequence((RandomUniform(-1,1), RandomUniform(-1,1)), i, 1, n)
with a recent version of GeoGebra RandomPointIn will also work
points = Sequence(RandomPointIn(-1, 1,-1, 1), i, 1, n)
With either of those you can get the estimate of pi as
p = 4 * CountIf(abs(A) < 1, A, points)/n
Upvotes: 0