In this tutorial, we will look at how to generate a random complex number in Python with the help of some examples.

## How to generate a random complex number in Python?

To generate a random complex number in Python, you can randomly generate the real and imaginary parts of the complex number. You can use Python built-in `random`

module to generate random numbers and the Python built-in `complex()`

constructor to combine them into a complex number.

The following is the syntax –

import random # randomly generate the real part x = random.randint(a, b) # randomly generate the imaginary part y = random.randint(a, b) # resulting complex number z = complex(x, y)

Here, we are randomly and independently generating the real and imaginary parts of the complex number using the `random.randint(a, b)`

function, which generates integer random numbers in the range `[a, b]`

(both inclusive).

You can also use the `random.random()`

function to get random numbers between `[0, 1)`

as real values and use them as your real and imaginary components.

## Examples

Let’s now look at some examples of using the above technique to generate random complex numbers in Python.

### Example 1 – Generate a random complex number having integer coefficients

Let’s use the `random.randint()`

function to get integer random values for our real and imaginary components.

import random # randomly generate the real part x = random.randint(0, 100) # randomly generate the imaginary part y = random.randint(0, 100) # resulting complex number z = complex(x, y) print(z)

Output:

(42+33j)

Here, we generate two random integers between 0 and 100 using the `random.randint()`

function and use them as the real and imaginary components of the resulting complex number.

### Example 2 – Generate a random complex number having real value coefficients

We can also generate real value random numbers using the `random`

module in Python.

For example, you can use the `random.random()`

function to generate real number random values in the range `[0, 1)`

. You can then scale and/or offset the resulting random value to get real number random values in any range.

Let’s now generate two real value random numbers in the range [0, 100) which will be our corresponding real and imaginary components.

import random # randomly generate the real part x = random.random() * 100 # randomly generate the imaginary part y = random.random() * 100 # resulting complex number z = complex(x, y) print(z)

Output:

(56.58859075750996+25.90352071828296j)

We get the resulting complex number. You can see that, here, we multiplied the result of `random.random()`

to scale it to the range of 0 to 100.

## Summary

In this tutorial, we looked at how to generate a random complex number in Python. The following is a summary of the steps mentioned in this tutorial –

- Randomly generate the real component.
- Randomly generate the complex component.
- Combine the real and generate complex components to get your random complex number.

You can use the `random`

module in Python to generate random numbers (both integers and real values) and the built-in `complex()`

constructor to create a complex number using these values.

You might also be interested in –

- Practical Guide to Working with Complex Numbers in Python
- Python – Add Two Complex Numbers
- Python – Subtract Two Complex Numbers
- Python – Multiply Two Complex Numbers
- Python – Divide Two Complex Numbers
- Python – Convert Complex Number to Polar Form
- Python – Get the Phase Angle of a Complex Number
- Python – Get the Absolute Value of a Complex Number
- Numpy – Get the Complex Conjugate
- Numpy – Get the Real Part of a Complex Number
- Numpy – Get the Imaginary Part of a Complex Number
- Numpy – Check If a Number is Real
- Numpy – Check If a Number is Complex

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**