Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Python»What are the Python Random Functions?

    What are the Python Random Functions?

    By RahulSeptember 10, 20201 Min Read

    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.

    Advertisement

    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

    Related Posts

    Setting and Getting the Default Timezone in Python

    Python Function with Parameters, Return and Data Types

    Understanding Python’s Underscore ( _ ): A Comprehensive Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting and Getting the Default Timezone in Python
    • What is Media Access Control (MAC) Address?
    • What is Cross-Site Scripting (XSS)?
    • What is Content Security Policy (CSP)?
    • A User’s Guide to Understanding Redirection Operators in Bash
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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