Summary of methods for generating random arrays using Python and summary of python Arrays

Source: Internet
Author: User
Tags shuffle

Summary of methods for generating random arrays using Python and summary of python Arrays

This example describes how to generate a random array using Python. We will share this with you for your reference. The details are as follows:

When studying the Sorting Problem, you often need to generate a random array to verify the correctness and performance of your sorting algorithm. Today, we will summarize the method for generating a random array in Python for future reference.

I. Use the random module to generate a random array

Python's random module has some methods to generate random numbers, such as random. randint, random. random, random. uniform, random. randrange: these functions are similar to each other. They all return an integer or floating point number within the specified range. Below we will briefly explain these functions.

1. random. randint (low, hight)-> return an integer between [low, hight ].

This function accepts two parameters, which must be an integer (or a floating point with 0 decimal places) and the first parameter must not be greater than the second parameter.

>>> import random>>> random.randint(1,10)5>>> random.randint(1.0, 10.0)5

2. random. random ()-> If the parameter is not accepted, a floating point number between [0.0, 1.0) is returned.

>>> random.random()0.9983625479554628

3. random. uniform (val1, val2)-> accept two numeric parameters and return a floating point number between the two numeric ranges. val1 is not required to be smaller than or equal to val2.

>>> random.uniform(1,5.0)2.917249424176132>>> random.uniform(9.9, 2)3.4288029275359024

* 4. random. randrange (start, stop, step)-> Returns a random integer from the start, stop, and step list. Similarly, the three parameters are all integers (or decimal places are 0). If start is greater than stop, setp must be negative. step cannot be 0. *

>>> Random. randrange (1,100, 2) # Return an odd number between [1,100]> random. randrange (100, 1,-2) # returns an even number 46 []

Run the following command:

5. Generate a random array

Next we use random. randint to generate a random array.

import randomdef random_int_list(start, stop, length):  start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))  length = int(abs(length)) if length else 0  random_list = []  for i in range(length):    random_list.append(random.randint(start, stop))  return random_list

Next we can use this function to generate a random integer sequence.

>>> random_int_list(1,100,10)[54, 13, 6, 89, 87, 39, 60, 2, 63, 61]

Ii. Use the numpy. random module to generate random arrays

1. np. random. rand is used to generate a random floating point number between [0.0, 1.0). If there is no parameter, a random floating point number is returned. If there is a parameter, a one-dimensional random floating point array with the length of this parameter is returned, the parameter is recommended to be an integer, because numpy may not support non-Integer Parameters in future versions.

import numpy as np>>> np.random.rand(10)array([ 0.56911206, 0.99777291, 0.18943144, 0.19387287, 0.75090637,    0.18692814, 0.69804514, 0.48808425, 0.79440667, 0.66959075])

Of course, this function can also be used to generate multi-dimensional arrays, which is not described here.

2. np. random. randn this function returns a sample with a standard normal distribution.

>>> np.random.randn(10)array([-1.6765704 , 0.66361856, 0.04029481, 1.19965741, -0.57514593,    -0.79603968, 1.52261545, -2.17401814, 0.86671727, -1.17945975])

3. np. random. randint (low [, high, size]) returns a random integer in the semi-open interval [low, high ).

>>> np.random.randint(10,size=10)array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])

4. random_integers (low [, high, size]) returns a random integer in the closed range [low, high].

>>> np.random.random_integers(5)4

5. np. random. shuffle (x) is similar to shuffling, disrupting the order. np. random. permutation (x) returns a random arrangement.

>>> arr = np.arange(10)>>> np.random.shuffle(arr)>>> arr[1 7 5 2 9 4 3 6 0 8]>>>> np.random.permutation(10)array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])

PS: Here are two related online tools for your reference:

Online random number/string generation tool:
Http://tools.jb51.net/aideddesign/suijishu

High-strength Password generator:
Http://tools.jb51.net/password/CreateStrongPassword

Related Article

Contact Us

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.