Close Menu
    Facebook X (Twitter) Instagram
    Trending
    • SharePoint Internal Column Name Encoded Character List
    • Satchel Pelts List for Red Dead Redemption 2
    • Create a Video Wall With FFmpeg
    • Mitsubishi Outlander Towbar Electrics
    • Mitsubishi Outlander Towbar Installation
    • Telephone Block List
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    YouTube Instagram Facebook RSS
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Development»Python»How To Generate Random Numbers In Python
    Coding Programming Guides

    How To Generate Random Numbers In Python

    0
    By Matt on April 17, 2013 Python, Tutorials & Help

    Python includes a standard library to generate random numbers. This library is called “random”. In order to start generating random numbers in Python you must import the random library at the start of your script.

    When you import the library the generator is seeded using the current system time so you may see different results every time you run your script.

    If you want the generator to always use the same sequence of numbers you can set the seed yourself to a known value using :

    random.seed(42)

    Where 42 is a number I chose at random. If I use this seed at the start of my script the results will always be the same.

    Like most computer systems the random numbers are “pseudo-random” in that give the same seed value they will always produce the same results. This may sound strange but can be quite useful for some purposes.

    If you want to ensure the results are different every time then use :

    random.seed()

    and the current system time will be used as the seed.

    Now for the most useful functions provided by the library.

    random.random()
    This generates a number between 0.0 and 1.0

    random.uniform(1.2, 2.5)
    This generates a number between 1.2 and 2.5.

    random.randint(3,9)
    This generates an integer (whole number) in the range 3 to 9.

    random.randrange(10)
    This generates an integer in the range 0 to 10.

    random.randrange(2,20,3)
    This generates an integer in the range 2 to 20 in steps of 3 (i.e. the set 2,5,8,11,14,17,20).

    random.choice([‘a’,’e’,’i’,’o’,’u’])
    This chooses an item from the supplied list of elements. In this case a list of letters.

    random.sample([12,16,19,34,67,23,34,56,67], 3)
    This chooses 3 items from the supplied list of elements. In this case a list of numbers.

    random.shuffle([1,2,3,4,5])
    This shuffles the contents of a list of items.

    In order to quickly see this functions in action you can use the simple script listed below. It prints out some randomly generated numbers using each of the functions listed above. You can adjust some of the parameters to see how it affects the result.

        # Import random library
        import random
    
        print "Generate some random numbers :"
    
        print "\nNumber in the range 0.0 to 1.0"
        print random.random()
    
        print "\nNumber in the range 1.2 to 2.5"
        print random.uniform(1.2, 2.5)
    
        print "\nWhole number in the range 3 to 9"
        print random.randint(3,9)
    
        print "\nNumber in the range 0 to 10"
        print random.randrange(10)
    
        print "\nNumber in the range 2 to 10 in steps of 3"
        print random.randrange(2,30,3)
    
        print "\nCharacter from the list ['a','e','i','o','u']"
        print random.choice(['a','e','i','o','u'])
    
        print "\nThree random numbers from list [12,16,19,34,23,34,56,67]"
        print random.sample([12,16,19,34,23,34,56,67], 3)
    
        print "\nShuffle contents of list [1,2,3,4,5]"
        mylist = [1,2,3,4,5]
        random.shuffle(mylist)
        print mylist
    

    Every time the script runs the random library is loaded and seeded with the current time. This results on the generated numbers being different. If you add “random.seed(42)” after the the import line your results will be the same every time the script is run.

    As an example you may want the results to be the same every time if you use the random number generator if you are dynamically generating content for a game (maps, dungeons, planets etc). This is also known as “Procedural Generation”.

    There are more functions available in the random library and these can be seen on the official Python “Generate pseudo-random numbers” page.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To Resize Images Using Python
    Next Article How To Remove Options From HTML Select Lists Using Javascript

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    109

    Recent Posts
    May 1, 2025

    SharePoint Internal Column Name Encoded Character List

    March 24, 2025

    Satchel Pelts List for Red Dead Redemption 2

    January 29, 2025

    Create a Video Wall With FFmpeg

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    Tags
    Android APEX Apple Arduino Black Friday Coolermaster csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling Format gaming GIMP Gritin Home Assistant Home Automation Linux lists Media os.stat os.walk Outlander Power Python Qi RDR2 Review scam Security SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10 Xbox One
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    YouTube Facebook Instagram Pinterest RSS

    Entries RSS | Comments RSS

    Copyright © 2025 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

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