# Random Walks and the Arcsine Law

Join the DZone community and get the full member experience.

Join For FreeSuppose you stand at 0 and flip a fair coin. If the coin comes up heads, you take a step to the right. Otherwise you take a step to the left. How much of the time will you spend to the right of where you started?

As the number of steps *N* goes to infinity, the probability that the proportion of your time in positive territory is less than *x* approaches 2 arcsin(√*x*)/π. The arcsine term gives this rule its name, the arcsine law.

Here’s a little Python script to illustrate the arcsine law.

```
import random
from numpy import arcsin, pi, sqrt
def step():
u = random.random()
return 1 if u < 0.5 else -1
M = 1000 # outer loop
N = 1000 # inner loop
x = 0.3 # Use any 0 < x < 1 you'd like.
outer_count = 0
for _ in range(M):
n = 0
position= 0
inner_count = 0
for __ in range(N):
position += step()
if position > 0:
inner_count += 1
if inner_count/N < x:
outer_count += 1
print (outer_count/M)
print (2*arcsin(sqrt(x))/pi)
```

Published at DZone with permission of John Cook, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments