Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
NumPy | Create Random Valued Array
Next article icon

NumPy | Create Random Valued Array

Last Updated : 09 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To create an array filled with random numbers, given the shape and type of array, we can use numpy.empty() method.

Example:

Python3
import numpy as np     b = np.empty(3, dtype = float)  print("Matrix b : \n", b)  

Output:

[6.79249278e-310 6.79249278e-310 1.69375695e+190]

Syntax

Syntax: np.empty(shape, dtype=None, order='C', *, like=None)

Parameter:

  • shape : Number of rows
  • order : C_contiguous or F_contiguous
  • dtype : [optional, float(by Default)] Data type of returned array.
  • like: [optional] allows you to create an array with the same shape and data type as another array-like object

More Examples

Let's see some examples of how to create an array with random values in NumPy.

Example 1

We create 2 NumPy arrays with random values in this example. Array 'b' with size 2 and array 'a' which is a 2D array.

Python3
# Python Program to create numpy array  # filled with random values import numpy as geek     b = geek.empty(2, dtype = int)  print("Matrix b : \n", b)     a = geek.empty([2, 2], dtype = int)  print("\nMatrix a : \n", a)  

Output:

Matrix b :    [140489599921032        21301024]    Matrix a :    [[140489599921048        18650592]   [       10738656 140489568798064]]  

Example 2

In this example, we create 2 random value arrays in NumPy. Array 'c' which is a 3x3 array and array 'd' which is a 5x3 array.

Python3
# Python Program to create numpy array  # filled with random values import numpy as geek   # Python Program to create numpy array  # filled with random values import numpy as geek   c = geek.empty([3, 3])  print("\nMatrix c : \n", c)  d = geek.empty([5, 3], dtype = int)  print("\nMatrix d : \n", d)    

Output:

Matrix c :    [[  1.37596097e-316   5.39314154e-317   5.39307830e-317]   [  5.39345774e-317   5.39345774e-317   6.93325440e-310]   [  5.39481741e-317   6.93325440e-310   8.69555537e-322]]    Matrix d :    [[140330665569272        23735792               0]   [       10739936 140330589556496               0]   [              0               0        10739904]   [140330587337872               0        10915968]   [              0        10739904               0]]  

Next Article
NumPy | Create Random Valued Array

S

Shivam_k
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-program
Practice Tags :
  • python

Similar Reads

    Numpy - Array Creation
    Numpy Arrays are grid-like structures similar to lists in Python but optimized for numerical operations. The most straightforward way to create a NumPy array is by converting a regular Python list into an array using the np.array() function.Let's understand this with the help of an example:Pythonimp
    5 min read
    Creating a one-dimensional NumPy array
    One-dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple. We can create a 1-D array in NumPy using the array() function, which converts a Python list or iterable object. Pythonimport numpy as np # Create a
    2 min read
    Select random value from a list-Python
    The goal here is to randomly select a value from a list in Python. For example, given a list [1, 4, 5, 2, 7], we want to retrieve a single randomly chosen element, such as 5. There are several ways to achieve this, each varying in terms of simplicity, efficiency and use case. Let's explore different
    2 min read
    numpy.random.standard_cauchy() in 1Python
    With the help of numpy.random.standard_cauchy() method, we can see get the random samples from a standard cauchy distribution and return the random samples. Standard cauchy distribution Syntax : numpy.random.standard_cauchy(size=None) Return : Return the random samples as numpy array. Example #1 : I
    1 min read
    Project Idea | ( True Random Number Generator)
    Introduction Random numbers in computer science is used for cryptography, simulation, sampling, design and games. In the past the need for more and more randomness has increased. Developers seek for more and more randomness. This project is based on generating random numbers using simple programming
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences