generate random complex number in python

Python – Generate a Random Complex Number

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?

generate 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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

(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 –

  1. Randomly generate the real component.
  2. Randomly generate the complex component.
  3. 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 –


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


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top