# Fill Array with Random Numbers in Python

## Fill array with random numbers in Python

An array is one of the fundamental data structures in Python. It stores elements in a contiguous memory location and each element can be accessed using its respective index. In Python, the `numpy` library is used to implement array data structures. Lists are also another data structure that mimics an array in Python.

In this tutorial, we will discuss different methods to fill array with random numbers in Python.

We can create an array of our desired shapes. Sometimes, we may require dummy arrays for some calculations. We can create an array filled with such dummy values since the `numpy` library also implements a `random` module to generate random numbers.

## Ways to fill array with random numbers in Python

Let us now discuss how to fill array with random numbers in Python.

### Using the `numpy.random.randint()` function to fill array with random numbers in Python

As discussed earlier, the `numpy` library has a `random` module that can help in generating random numbers for `numpy` arrays. The `randint()` function can be used to generate an array of the required size and fill array with random numbers in Python.

We need to remember the three main parameters. The first two parameters are the `low` and `high` values. The function will select a random integer between this range. The third parameter is the `shape` parameter which specifies the shape of the final array.

See the following example.

Output:

[3 8 1 0 4 2 4 7 0 3]

In the above example, we generate random numbers between 0 to 10 and fill it in a one-dimensional array of length ten.

### Using the `numpy.random.Generator.integers()` function to fill array with random numbers in Python

The `numpy.random.Generators` offer a new way to generate and work with random numbers. It uses an additional `BitGenerator` to spawn such random bits and manage their states. To initiate a new `Generator`, we use the `numpy.random.default_rng()` constructor.

After this, we can use the `numpy.random.Generator.integers()` function to generate random numbers and fill array with random numbers in Python.

We need to specify the `low`, `high`, and `shape` values in this function as we did in the previous method.

For example,

Output:

[0 2 1 1 1 0 1 1 3 0]

### Using the `random.randint()` function to fill array with random numbers in Python

As discussed in the first section, lists in Python also mimic an array. We can fill lists with random numbers using the `random` module in Python. We will use list comprehension along with the `random.randint()` function to fill array with random numbers in Python.

We will use the `random.randint()` function to generate a random number between a given range. We will use the list comprehension method that loops over this function the required number of times, creating a new list of the required length.

See the following code.

Output:

[4, 2, 5, 3, 2, 2, 3, 3, 4, 2]

## Conclusion

To conclude, in this tutorial we discussed different methods to fill array with random numbers in Python. Essentially, we were creating arrays with random numbers. For this, we used `numpy` arrays and lists.

For `numpy` arrays, we had two methods. The first was the traditional `numpy.random.randint()` function that generates a `numpy` array of a given length filled with random numbers between a given range.

In the second method, we used a relatively new `numpy.random.Generators` module to create the array. It uses `Generators` that provide an additional state to manage and generate random bits.

In the final method, we discussed how to fill array with random numbers in Python using the list comprehension method and the `random.randint()` function. Essentially, we create a loop and run the `randint()` function the required number of times and add the generated number to a list.

import_contacts

## Related Posts

• 17 May

• 03 May

### Create an Array of 1 to 10 in Python

Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

• 30 December

### How to Append to Array in Python

Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusionWas this post helpful? In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how […]

• 17 December

### Python array size: get size of array in Python

Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusionWas this post helpful? In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. […]

• 11 November

### Convert float array to int array in Python

Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusionWas this post helpful? What are the types of arrays? An array is a common structure […]

• 01 November

### Convert Bytearray to String in Python

Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusionWas this post helpful? Python supports different types of sequence objects to store data. One such […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.