Skip to content

Instantly share code, notes, and snippets.

@jnory
Last active June 2, 2018 12:50
Show Gist options
  • Save jnory/7e9b04a0e1f19f51043b69c13fa0bba8 to your computer and use it in GitHub Desktop.
Save jnory/7e9b04a0e1f19f51043b69c13fa0bba8 to your computer and use it in GitHub Desktop.
円周率
import numpy as np
import pandas as pd
def main(args):
np.random.seed(args.seed)
index = []
history = []
n_within_circle = 0
for i in range(args.iter):
sample = 2.0 * np.random.random(2) - 1.0
if sample.dot(sample) < 1.0:
n_within_circle += 1
total = i + 1
if (total * 10) % args.interval == 0:
assumed_pi = 4 * n_within_circle / total
index.append(total)
history.append(assumed_pi)
if total % args.interval == 0:
print("{}/{} assumed_pi={}".format(
n_within_circle, total, assumed_pi))
ax = pd.DataFrame(history, index=index).plot(
ylim=[3, 3.3], legend=False, logx=True)
fig = ax.get_figure()
fig.savefig(args.out)
def get_parser():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--iter", default=1000000, type=int)
parser.add_argument("--interval", default=10000, type=int)
parser.add_argument("--seed", default=13, type=int)
parser.add_argument("--out", default="pi_history.png")
return parser
if __name__ == '__main__':
main(get_parser().parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment