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 :
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 :
and the current system time will be used as the seed.
Now for the most useful functions provided by the library.
This generates a number between 0.0 and 1.0
This generates a number between 1.2 and 2.5.
This generates an integer (whole number) in the range 3 to 9.
This generates an integer in the range 0 to 10.
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).
This chooses an item from the supplied list of elements. In this case a list of letters.
This chooses 3 items from the supplied list of elements. In this case a list of numbers.
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.