# Use of Rand (), RANDN (), Randint (), random_integers () in Python's NumPy library

Source: Internet
Author: User

1.numpy.random.rand ()
Usage is: Numpy.random.rand (d0,d1,... DN)
Creates an array with the given shape and adds a random sample evenly distributed between [0,1] in the array.
usage and implementation :

`>>> Np.random.rand (3,2) Array ([[0.14022471,  0.96360618],  #random       [0.37601032,  0.25528411],  #random       [0.49313049,  0.94909878]]) #random`

`>>>np.random.rand (5) array ([0.26677034,  0.01680242,  0.5164905,  0.70920141,  0.30438513])`

2.numpy.random.randn ()
Usage is: Numpy.random.rand (d0,d1,... DN)
Creates an array of array elements in the given shape to conform to the standard normal distribution N (0,1)
To get a general normal distribution, you can use sigma * NP.RANDOM.RANDN (...) + mu to represent
usage and implementation :

`>>> a = NP.RANDOM.RANDN (2, 4) >>> Aarray ([[ -0.29188711],  0.76417681,  1.00922644  , 0.34169581],       [ -0.3652463, -0.9158214,  0.34467129, -0.31121017]]) >>> B = Np.random.randn (2) >> > Barray ([0.37849173,  1.14298464])`

3.numpy.random.randint ()
Usage is: numpy.random.randint (low,high=none,size=none,dtype)
Generates an integer value that is uniformly distributed on the semi-open half-closed interval [Low,high] ; if High=none, the range of values becomes [0,low]
usage and implementation
The situation of High=none

`>>> a = Np.random.randint (2, size=10) >>> Aarray ([0, 1, 0, 1, 1, 0, 1, 0, 0, 1]) >>> B = Np.rando  M.randint (1, size=10) >>> Barray ([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) >>> C =  np.random.randint (5, size= (2, 4)) >>> CArray ([[3, 4, 3, 3],       [3, 0, 0, 1]])`

High≠none

`D = Np.random.randint (2,high=6,size= (2,4)) >>> Darray ([[5, 2, 4, 2],       [4, 3, 5, 4]])`

4.numpy.random.random_integers ()
Usage is: numpy.random.random_integers (low,high=none,size=none)
Generates integer values for discrete uniform distributions on closed intervals [Low,high] ; if High=none, the range of values becomes [1,low]
usage and implementation
The situation of High=none

`>>> np.random.random_integers (1, 6,) array ([4, 5, 2, 3, 4, 2, 5, 4, 5, 4]) >>> Np.random.random_integer S (6) 5>>> np.random.random_integers (6,size= (3,2))Array ([[1, 3],[5, 6],[3, 4]])`

The situation of High≠none

`>>> C =  np.random.random_integers (6,high=8,size= (3,2)) >>> CArray ([[[7, 8],       [7, 8],       [8, 8])`

In addition, to divide the "A, B" interval into equal n, you can also use this function to implement
A + (b-a) * (Numpy.random.random_integers (N)-1)/(N-1)

5.numpy.random_sanmple ()
Usage is: numpy.random.random_sample (size=none)
Returns a random floating-point number between [0,1] in a given shape
usage and implementation

`>>> np.random.random_sample () 0.2982524530687424>>> np.random.random_sample ((5,)) Array ([ 0.47989216,  0.12580015,  0.99624494,  0.14867684,  0.56981553]) >>> np.random.random_ Sample ((2,5)) array ([[0.00659559,  0.45824325,  0.13738623,  0.60766919,  0.39234638],       [ 0.6914948,  0.92461145,  0.43289058,  0.63093292,  0.06921928]])`

Other functions,numpy.random.random () ; numpy.random.ranf ()
numpy.random.sample () usage and implementation are the same as it

6.numpy.random.choice ()
Usage is: Numpy.random.choice (a,size=none,replace=true,p=none)
If a is an array, the element is selected from a, and if A is a single int type, the number in range (a) is selected
Replace is a bool type, true, the selected element repeats, and no duplicates occur
P is an array, which holds the possibility of selecting each number, that is, the probability
usage and implementation

`>>>a =  Np.random.choice (5, 3) >>> Aarray ([4, 3, 1]) >>>b =  Np.random.choice (5, 3, p=[  0.1, 0, 0.3, 0.6, 0]) >>> barray ([2, 3, 3], dtype=int64) >>> C =  np.random.choice (5, 3, Replace=false, p=[0.1, 0, 0.3, 0.6, 0]) >>> carray ([3, 2, 0])`

Use of Rand (), RANDN (), Randint (), random_integers () in Python's NumPy library

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.