diff --git a/notebooks/numpy_cours.ipynb b/notebooks/numpy_cours.ipynb index fe632fbd4ddb438dd5d3543aec7b68b374844ed9..19ff1ffcc87c622ea13515b3df5a1a339afae67b 100644 --- a/notebooks/numpy_cours.ipynb +++ b/notebooks/numpy_cours.ipynb @@ -1033,6 +1033,171 @@ "> Python language has its own random module but numpy has more functionalities." ] }, + { + "cell_type": "markdown", + "id": "e73ecd23-a1eb-47ef-be1f-f92130547340", + "metadata": {}, + "source": [ + "First you have to create a Generator object, usualy by using the default_rng function" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "8e18d829-b9e4-4880-b35e-a96fc713d95f", + "metadata": {}, + "outputs": [], + "source": [ + "rng = np.random.default_rng() " + ] + }, + { + "cell_type": "markdown", + "id": "55fbac7f-33d1-4108-9f50-6aaf6db640a7", + "metadata": {}, + "source": [ + "then use random methods" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "81be533a-3beb-4af8-9e26-69ad5d44e41d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.9770692753749981" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Generate one random float uniformly distributed over the range [0, 1)\n", + "rng.random() # result below may vary" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f085514b-5fcd-4112-a045-23eb6f6f3e5c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1.73553135, 0.22361916, -0.73362824, -0.33249582, -0.72058886,\n", + " -0.27996662, -0.34853205, -2.27085731, -0.80546885, 1.42012754])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# array of normally distributed values\n", + "# with mean=0 (loc) and std=1.0 (scale) (defaults)\n", + "rng.normal(loc=0.0, scale=1.0, size=10)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9dcf97f4-1984-4739-a49a-9c9c7a7d4a98", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[-0.25121824, -1.50960959],\n", + " [ 1.20405797, 1.76066421],\n", + " [ 1.00618911, 1.00338646],\n", + " [ 0.33048819, 3.14524965],\n", + " [ 4.26693195, 1.77804119]])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# a 2Darray of normally distributed values\n", + "rng.normal(loc=1.0, scale=2.0, size=(5,2))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "8faa1d85-e2cf-482f-af13-1a339f85b273", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1.47566294, 1.66996391, 0.97314979, 0.11779833, 1.43473446,\n", + " 0.21334751, 0.67050575, 1.75677774, 0.95711036, 0.97817719])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# array of uniform distributed values\n", + "rng.uniform(low=0, high=2, size=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3494d529-5fef-47ef-9014-64b6d3d69244", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.08564916714362436" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# you can specify a seed when you build a random generator\n", + "s_rng = np.random.default_rng(seed=3)\n", + "\n", + "s_rng.random()" + ] + }, + { + "cell_type": "markdown", + "id": "2d2948e4-3d1e-4c22-b5e9-b92889282bf5", + "metadata": {}, + "source": [ + "* https://numpy.org/doc/stable/reference/random/index.html\n", + "* https://numpy.org/doc/stable/reference/random/generator.html#numpy.random.default_rng\n", + "* https://numpy.org/doc/stable/reference/random/generator.html#numpy.random.Generator" + ] + }, + { + "cell_type": "markdown", + "id": "2c0eea6a-b4d2-4c28-9674-af82879f6101", + "metadata": {}, + "source": [ + "## old way to generate random values\n", + "\n", + "These functions will be deprecated soon" + ] + }, { "cell_type": "code", "execution_count": 36, @@ -2958,7 +3123,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.12" + "version": "3.10.12" } }, "nbformat": 4,