Mostreig aleatori en numpy | funció randint().

numpy.random.randint()> és una de les funcions per fer mostreig aleatori a numpy. Retorna una matriu de forma especificada i l'omple amb nombres enters aleatoris de baix (incloent) a alt (exclusiu), és a dir, en l'interval [low, high).>

Sintaxi: numpy.random.randint (baix, alt = Cap, mida = Cap, dtype = 'l')

Paràmetres:
baix : [int] Enter més baix (signe) que s'ha de treure de la distribució. Però, funciona com un nombre enter més alt de la mostra si alt=Cap.
alt: [int, opcional] El nombre enter més gran (signe) que s'ha de treure de la distribució.
mida: [int o tupla d'ints, opcional] Forma de sortida. Si la forma donada és, per exemple, (m, n, k), llavors es dibuixen m * n * k mostres. El valor per defecte és Cap, en aquest cas es retorna un únic valor.
dtype: [opcional] Tipus de dades de sortida desitjat.

Tornada: Matriu de nombres enters aleatoris a l'interval [low, high)> o un sol int aleatori si no es proporciona la mida.

Codi #1:




# Python program explaining> # numpy.random.randint() function> > # importing numpy> import> numpy as geek> > # output array> out_arr> => geek.random.randint(low> => 0> , high> => 3> , size> => 5> )> print> (> 'Output 1D Array filled with random integers : '> , out_arr)>

Sortida:

 Output 1D Array filled with random integers : [1 1 0 1 1] 

Codi #2:




# Python program explaining> # numpy.random.randint() function> > # importing numpy> import> numpy as geek> > > # output array> out_arr> => geek.random.randint(low> => 4> , size> => (> 2> ,> 3> ))> print> (> 'Output 2D Array filled with random integers : '> , out_arr)>

Sortida:

 Output 2D Array filled with random integers : [[1 1 0] [1 0 3]] 


Codi #3:




# Python program explaining> # numpy.random.randint() function> > # importing numpy> import> numpy as geek> > # output array> out_arr> => geek.random.randint(> 2> ,> 10> , (> 2> ,> 3> ,> 4> ))> print> (> 'Output 3D Array filled with random integers : '> , out_arr)>

Sortida:

 Output 3D Array filled with random integers : [[[4 8 5 7] [6 5 6 7] [4 3 4 3]] [[2 9 2 2] [3 2 2 3] [6 8 3 2]]]