{ "cells": [ { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Pseudorandom number generation\n", "\n", "# Generate a *sequence* of numbers\n", "# Initially start the sequence at x=1\n", "\n", "# 1 is called the *seed* of the pseudorandom number generator\n", "\n", "x = 1\n", "\n", "# By default, Python uses the current time as the seed" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.2477936010098986e-05\n", "0.08503244914348818\n", "0.6013526053174179\n", "0.8916112770753034\n", "0.9679557019695433\n", "0.18968977182623453\n", "0.514975824167475\n", "0.39800838818680884\n", "0.26290616545030204\n", "0.7435124515292758\n" ] } ], "source": [ "for i in range(10):\n", " x = x*48271 % 2147483647\n", " print(x / 2147483647)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.13436424411240122\n", "0.8474337369372327\n", "0.763774618976614\n", "0.2550690257394217\n", "0.49543508709194095\n", "0.4494910647887381\n", "0.651592972722763\n", "0.7887233511355132\n", "0.0938595867742349\n", "0.02834747652200631\n" ] } ], "source": [ "import random\n", "\n", "random.seed(1) # Set once at the beginning of your code\n", "\n", "for i in range(10):\n", " print(random.random())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.5.1" } }, "nbformat": 4, "nbformat_minor": 2 }