teaching:exos:simulations_random_walks_codes

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
Prochaine révisionLes deux révisions suivantes
teaching:exos:simulations_random_walks_codes [2013/11/14 09:56] villersdteaching:exos:simulations_random_walks_codes [2018/11/05 11:37] – [Histogrammes de nombres aléatoires] villersd
Ligne 5: Ligne 5:
 ===== Génération de nombres aléatoires ===== ===== Génération de nombres aléatoires =====
  
-<sxh python; title : 01_random.py> +<code python 01_random.py> 
-#!/usr/bin/env python+#!/usr/bin/python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
 +"""
 +cf. documentation cf http://docs.python.org/library/random.html 
 +random number generation - génération de nombres aléatoires
 +functions of interest : choice, randint, seed
 +"""
  
 from random import *  from random import * 
-# cf. documentation cf http://docs.python.org/library/random.html  
-# random number generation - génération de nombres aléatoires 
-# functions of interest : choice, randint, seed 
  
-facepiece=['pile','face'+facepiece = ['pile','face'
-valeurpiece=[0.01,0.02,0.05,0.1,0.2,0.5,1.,2.]+valeurpiece = [0.01,0.02,0.05,0.1,0.2,0.5,1.,2.]
  
-#for i in range(1):+for i in range(1):
     # choice : random choice of an element from a list     # choice : random choice of an element from a list
-    #print choice(facepiece), choice(valeurpiece)+    print(choice(facepiece), choice(valeurpiece))
     # randint : return a random integer number between 2 values (including limits)     # randint : return a random integer number between 2 values (including limits)
-    #print randint(0,10)       # imprime un nombre aléatoire entre 0 et 10 +    print(randint(0,10))       # imprime un nombre aléatoire entre 0 et 10 
-    #print choice(range(0,11,1))  # same function, using choice and range to create the list+    print(choice(range(0,11,1)))  # same function, using choice and range to create the list 
 +    
  
 # seed(ANY_DATA) : seeding of the random number generator with any (constant) data # seed(ANY_DATA) : seeding of the random number generator with any (constant) data
Ligne 32: Ligne 35:
     seed()   # to randomly initiate the generator     seed()   # to randomly initiate the generator
     for i in range(10):     for i in range(10):
-        print randint(1000,9999) +        print(randint(1000,9999)
-    print " " +    print(" ") 
-</sxh>+</code>
  
 ===== Histogrammes de nombres aléatoires  ===== ===== Histogrammes de nombres aléatoires  =====
  
-<sxh python; title : 02_random_histogram.py>+<code python 02_random_histogram.py>
 #!/usr/bin/env python #!/usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
Ligne 50: Ligne 53:
 seed() seed()
  
-rval=[] +rval = [] 
-for j in range(10000):+for j in range(100000):
     rval.append(randint(0,99))   # append to the list a random (integer) number between 0 and 99     rval.append(randint(0,99))   # append to the list a random (integer) number between 0 and 99
  
Ligne 58: Ligne 61:
 # analysis - histogram  -  see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html # analysis - histogram  -  see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html
 # http://fr.wikipedia.org/wiki/Histogramme # http://fr.wikipedia.org/wiki/Histogramme
-xh=np.array(rval)  # see http://www.scipy.org/Cookbook/BuildingArrays  transforme une liste en un tableau numérique de Numpy +xh = np.array(rval)  # see http://www.scipy.org/Cookbook/BuildingArrays  transforme une liste en un tableau numérique de Numpy 
-# print xh+# print(xh)
  
 fig = plt.figure() fig = plt.figure()
 ax = fig.add_subplot(111) ax = fig.add_subplot(111)
  
-n, bins, patches = ax.hist(xh, 10, facecolor='green', alpha=0.75) +n, bins, patches = ax.hist(xh, 50, facecolor='green', alpha=0.75) 
-print n  # les nombres d'occurences par classe +print(n # les nombres d'occurences par classe 
-print bins  # les classes, de largeur identique+print(bins # les classes, de largeur identique
  
 # modifier le nombre de nombres générés, les nombres de classes-bins,  # modifier le nombre de nombres générés, les nombres de classes-bins, 
  
 plt.show() plt.show()
-</sxh>+ 
 +</code>
  
 ===== Représenter le déplacement d'un objet  ===== ===== Représenter le déplacement d'un objet  =====
  • teaching/exos/simulations_random_walks_codes.txt
  • Dernière modification : 2018/11/05 12:09
  • de villersd