The personal wiki of ...
Picking a Random Number

Python comes with libraries of functions that do almost anything.

Let's tell Python to pick a random number. Get the "random" library by typing this:

> >> import random
> >>

Now we can say "random.randint(1,100)" to pick a random number from 1 to 100.

> >> random.randint(1,100)
95 > >>

Which number did you get?

You can learn about libraries by saying help. Try typing this:

> >> help(random)
Help on module random:

NAME random - Random variable generators. (...and on and on...)

It will show lots of information, and you can press the space bar to see a screenful at a time. Eventually you will see a long list of all the functions in the library.

We don't have time to read it all now, so you can press "q" when you have seen enough.

You can use help() to ask about most things in python.

Next
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 20 Jan 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated