COSC 235: Programming and Problem Solving
April 8, 2009

Joseph D. Sloan
sloanjd @ wofford.edu


Random numbers continued

Pseudo random numbers and seeds
from random import *
randrange(1,11)   # random integer from 1 thru 10 inclusive 
random()          # random float between 0 and 1 


Examples

Rounding idioms

2 * int(i)                       # will be even
if x % 2 != 0: x += 1            # will round an integer up to next even number
if x % 3 != 0: x += (3 - x % 3)  # will round an interger up to next multiple of 3  

Extended Example with Tennis

First Try
Second Try
Third Try

Homework

Give the appropriate Python code to generate random numbers in the following range:

1. an integer between 1 and 5 inclusive
2. an integer between 0 and 5 inclusive
3. an integer between -10 and 10 inclusive
4. a floating point number between 0 and 10
5. a floating point number between -2 and 5


This page was created by Joe Sloan.
It was last modified on or after: 10 April 2009
Send mail to: sloanjd@wofford.edu