Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Python»What are the Python Random Functions?

    What are the Python Random Functions?

    RahulBy RahulSeptember 9, 20201 Min ReadUpdated:September 10, 2020

    Python provide multiple functions to generate random numbers. Most of these functions are available under the Python random module. You can use these functions for generating random numbers in your Python scripts.

    Here is the five main functions used for generating random numbers in a Python script.

    1. random()
    2. randint()
    3. uniform()
    4. randrange()
    5. choice()

    1. Python random() function:

    This function returns a random floating point value between 0 to 1. You can also specify the range to override the defaults.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import random
     
    # Print a random float number between 0 and 1
    x = random.random()
    print (x)
     
    # Print a random float number between 0 and 10
    x = random.random() * 10
    print (x)
     
    # Print a random float number between -5 and +5
    x = random.random() * 10 - 5
    print (x)

    2. Python randint() function:

    The randint() function takes in a range and produces an integer number between the defined range.

    1
    2
    3
    4
    5
    import random
     
    # Print a random integer between 10 and 100
    x = random.randint(10, 100)
    print (x)

    3. Python uniform() function:

    Just as the randint() function generates an integer within a given range, uniform() does the same for floating point numbers.

    1
    2
    3
    4
    5
    import random
     
    # Print a floating point number between 10 and 50
    x = random.uniform(10, 50)
    print (x)

    4. Python randrange() function:

    The randrange() function is used to select a integer value between a defined range. You can also specify to select even or odd number between a range.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import random
     
    # Print a Integer number between 0 to 9
    x = random.randrange(10)
    print (x)
     
    # Print a integer number between 10 to 99
    x = random.randrange(10, 100)
    print (x)
     
    # Print a Even integer number between 10 to 99
    x = random.randrange(10, 100, 2)
    print (x)

    5. Python choice() function:

    Python choice() function is used to select a single random element from a sequence.

    1
    2
    3
    4
    5
    import random
     
    # Select a random element from below sequence
    x = random.choice(['red', 'geen', 'yellow'])
    print (x)

    functions Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install NVM on Fedora 35/34/33
    Next Article How to Install Let’s Encrypt (Certbot) on CentOS 8

    Related Posts

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    Setup Selenium with Python and Chrome on Fedora

    Updated:June 18, 20223 Mins Read

    Creating Python Virtual Environment on Windows

    Updated:June 3, 20222 Mins Read

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    How To Install Python 3.10 on Debian 11/10

    2 Mins Read

    How to Install Python 3.10 on CentOS/RHEL 8 & Fedora 36/35

    Updated:June 6, 20223 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.