You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

78 lines
1.6 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* **下面用python来实现随机投点法计算圆周率**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入总的投点数:99999999\n",
"pi的值是3.1413824314138243\n"
]
}
],
"source": [
"from random import random\n",
"darts = int(input(\"请输入总的投点数:\"))\n",
"hits = 0 \n",
"for i in range(darts):\n",
" x, y = random(), random()\n",
" if (x**2 + y**2) < 1:\n",
" hits = hits + 1\n",
"pi = 4 * (hits / darts)\n",
"print(\"pi的值是{}\".format(pi))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}