{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Random generator parametrization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The seed of the random generator is initialized to 0 when the module is imported.\n",
    "\n",
    "It means the same script will yield the same results across several replications.\n",
    "\n",
    "It is also possible to initialize the random generator differently."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import os\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 0 : using a fixed seed\n",
    "ot.RandomGenerator.SetSeed(77)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example 1 : using the python process id\n",
    "ot.RandomGenerator.SetSeed(os.getpid())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example 2 : using the time in milliseconds\n",
    "ot.RandomGenerator.SetSeed(int(1000*time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.210584]\n",
      "[0.210584]\n"
     ]
    }
   ],
   "source": [
    "# Example 3 : using a previously saved generator state\n",
    "\n",
    "# use the random generator\n",
    "ot.Normal().getSample(100)\n",
    "# save the generator state\n",
    "particularState = ot.RandomGenerator.GetState()\n",
    "print(ot.Normal().getRealization())\n",
    "\n",
    "# load the generator state\n",
    "ot.RandomGenerator.SetState(particularState)\n",
    "print(ot.Normal().getRealization())"
   ]
  }
 ],
 "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
