1 / 11

Random number generator

Random number generator. Learning objective: Use a random number generator. Packages. Before we proceed it is worth mentioning packages. Packages contain additional functions that are not part of standard Python. Useful packages include:

dyvonne
Download Presentation

Random number generator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Random number generator Learning objective: Use a random number generator

  2. Packages Before we proceed it is worth mentioning packages. Packages contain additional functions that are not part of standard Python. Useful packages include: • random – package that contains function for generating random numbers. • Math – package that contains lots of mathematical functions including log, sin, cos and so on. • datetime – package that contains lots of date and time routines. • Time – for timeing running of programs for instance To use a package, at the top of the program write import <package name>

  3. Datetime and Time packages How to get current date and time in Python using datetime package import datetime t=datetime.datetime.today() print(t.hour) print(t.year) print(t.month) print(t.day) How pause a program: import time Time.sleep(1)

  4. Maths package Some common maths function. Need to insure values are give in radians not degrees for the relevant functions. import math math.pi math.sin(math.radians(20)) math.cos(math.radians(20)) math.pow(5,2)

  5. Using the random function The random function is a very useful function that is used in many places in programming and is especially useful for solving complex mathematical problems. In Python to use the random function you need to first import the random package. Packages contain additional functions that are not part of standard Python. import random num=random.randint(1,10) randint will select a value between the low value and the high value in this case 1 and 10 respectively print(num)

  6. Use the random function to write a program to simulate the rolling of a die # Roll die # 22 June 2015 import random whileTrue: input("Roll die") print(random.randint(1,6))

  7. Maths quiz Write a program that generates random maths questions for adding, subtracting, multiplying or dividing pairs of numbers.

  8. # Maths quiz. # 11th April 2018 import random print("Maths quiz") score=0 foriin range(10): num1=random.randint(1,10) num2=random.randint(1,10) print("What is "+str(num1)+" + "+str(num2)+"?") total=num1+num2 answer=int(input()) ifint(total)==answer: print("Well done") score=score+1 else: print("Wrong the answer is "+str(total)) print("Your final score is"+ score)

  9. Guessing game Write a program for a guessing game. The computer selects a random integer between 1 and 100. The user tries to guess the number. The computer informs the user if the number is too high or too low. The user keeps guessing until the guess is correct. Extension: • The program tells the user how many guesses they have had. • The user has the option to play again • Find out what is the most effective strategy for playing the guessing game?

  10. Guessing game # Simple guessing game program # 22 June 2015 import random x=random.randint(1,100) n=0 guess=0 while guess!=x: guess=int(input("Guess a number between 1 and 100?: ")) if(guess > x): print(guess,"is too high") if(guess < x): print(guess,"is too low") if guess==x: print("Well done! the answer was ", x)

  11. Create a magic 8 ball program

More Related