Random numbers What does it mean for a number to be random? A number that is drawn from a set of possible values, each of which is equally probable. More precisely, the outcome is non-predictable. Many practical uses: Computer games Simulation of natural phenomena
Cryptography Art, Music Etc any problem where you want to produce an unpredictable result! The random module Thankfully, you don't have to implement your own RNG import random from random import *
Some random functions choice(my_list) chooses 1 element from the sequence my_list choice(['Harris', 'McDonnell', 'Larison']) How would you get a random int from 0 to 9 inclusive? randint(low,hi)
chooses a random int from low to hi, inclusive Throw a coin n times from random import * def flip_coins(n = 20): mapping = ['H', 'T'] flip_list = [ mapping[randint(0,1)] for flip in range(n) ] print(''.join(flip_list))
This will do the same from random import * def flip_coin2(n = 20): flip_list = [ choice([H, T]) for flip in range(n) ] print(''.join(flip_list)) The above two versions of the program prints the list, how to return the list instead of printing? Example: random, chr(), ord()
Given a string, write a function that converts all letters to upper case, keeping others as they are. Note: without using upper(), i.e., write your own upper(). Example: abc123 ABC123, hello HELLO, , 123 456 123 456 Idea: First check to see if the parameter is a letter a z. If it is not, return as it is; If it is a lower case letter, compute the distance between this letter and a, return the letter by adding the distance to A. Do it for every symbol in the string.
upper_letter() to_upper() and its use A bit twist The same as above, converting to upper case, however, now we want to map each lower case letter to a random upper case letter. Example: abc123 SGC123, hello SGEBU, 123 456 , 123 456 123 456
Idea: everything is the same as the previous case (to_upper()) except when generating the upper case letter, we use a random distance, instead of a fixed one [ord(c) ord(a)] Code and execution Monte Carlo Methods Any method which solves a problem by generating suitable random numbers, and observing the fraction of numbers obeying
some property (or properties) The method is useful for obtaining numerical solutions to problems which are too complicated to solve analytically. http://mathworld.wolfram.com/MonteCarloMethod.html Monte Carlo in action How many doubles will you get in n rolls of 2 dice? the input n is the total number of
rolls def count_doubles(n): """ inputs a # of dice rolls outputs the # of doubles """ if n == 0: return 0 # zero rolls, zero doubles else: d1 = choice([1,2,3,4,5,6]) one roll
d2 = choice(range(1,7)) if d1 != d2: return count_doubles(n-1) # don't count it else: return 1 + count_doubles(n-1) # COUNT IT! where is the doubles Monte Carlo in action Write the same function with
a list comprehension def count_doubles(n): """ inputs a # of dice rolls outputs the # of doubles """ return sum([choice(range(6)) == choice(range(6)) \ for x in range(n)]) where is the doubles O, ne final example Suppose you wanted to estimate pi
1. Generate two random numbers between -1 and 1. denoted (x,y) 2. Compute the distance of (x,y) from (0,0) 3. Depending on distance, point is either inside or outside of a circle centered at (0,0) with a radius of 1. Illustration Exercise
Design a strategy for estimating pi using random numbers ("dart throws") and this diagram ("dartboard"): Name(s): 1) Here is a dart-throwing function: (1,1) What does
this return? def throw(): return [ random.uniform(-1,1), random.uniform(-1,1) ] 2) Here is a dart-testing function: What does this return?
def test(x,y): return (x**2 + y**2) < 1.0 3) What strategy could use the functions in (1) and (2) to estimate pi? (-1,-1) 4) Write a function to implement your strategy:
Learning Methods Explain the role of sampling in the research process Distinguish between probability and nonprobability sampling Understand the factors to consider when determining sample size Understand the steps in developing a sampling plan Value of Sampling in Marketing Research...
Editor and Publishing Environment. Web map product. QA/QC. Internal and from other government agencies. ... SPC- Geoscience Division. We would just like to acknowledge all the custodians whose data enabled this product to come together. Thank you. Author:
Household durable goods used for household unincorporated enterprise must be allocated to Gross Domestic Capital Formation (GDCF) ... Addition to cultivated crops harvested repeatedly, such as fruit crops, rubber plants, coconut plants, etc.
Michigan IRP Gas Forecast In the following slide, you will see 3 natural gas profiles EIA WellHead Price for the Lower 48 Average Published data from the EIA EIA WellHead Price adjusted upward by 12.2% to account for the median...
Heavier load of rock and shingle is deposited on the backshore. Finer, lighter material such as sand is deposited on the foreshore. Over time a beach is formed. A storm beach is made of pebbles and stones. It forms when...
Ready to download the document? Go ahead and hit continue!