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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- 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.