Lecture 6
Lecture 6
1
Pseudorandom number generators
• Some problems we want to program need to simulate a
random choice.
• Examples: flip of a coin, roll of a dice
2
Pseudorandom number generators
Linear congruential method:
• xn+1 = (a.xn + c) mod m
Example:
• Assume : m=9,a=7,c=4, x0 = 3
Hash functions
A hash function is an algorithm that maps data of arbitrary length
to data of a fixed length.
The values returned by a hash function are called hash values or
hash codes.
Example:
Hash function
John 00
01
Mary
02
Peter 03
Ann 04
..
Charles 19
3
Hash functions
• Problem: Given a large collection of records, how can we
store and find a record quickly?
• Solution: Use a hash function calculate the location of the
record based on the record’s ID.
• Example: A common hash function is
• h(k) = k mod n,
where n is the number of available storage locations.
0 1 2 3 4 5 6 7 8
M. Hauskrecht
Hash function
An example of a hash function that maps integers (including very
large ones) to a subset of integers 0, 1, .. m-1 is:
h(k) = k mod m
4
Hash functions
• Problem: two documents mapped to the same location
0 1 2 3 4 5 6 7 8
M. Hauskrecht
Hash functions
• Solution 1: move the next available location
– Method is represented by a sequence of hash functions to
try h (k) = k mod n
0
h1(k) = (k+1) mod n
…
hm(k) = (k+m) mod n
0 1 2 3 4 5 6 7 8
M. Hauskrecht
5
Hash functions
• Solution 2: remember the exact location in a secondary
structure that is searched sequentially
ID: 39
Loc: 4
ID: 21
Loc: 3
0 1 2 3 4 5 6 7 8
M. Hauskrecht
Cryptology
Encryption of messages.
• Ceasar cipher:
• Shift letters in the message by 3, last three letters mapped to the
first 3 letters, e.g. A is shifted to D, X is shifted to A
How to represent the idea of a shift by 3?
• There are 26 letters in the alphabet. Assign each of them a
number from 0,1, 2, 3, .. 25 according to the alphabetical order.
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
6
Cryptology
Encryption of messages using a shift by 3.
• The encryption of the letter with an index p is represented as:
• f(p) = (p + 3) mod 26
Coding of letters:
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
• Encrypt message:
– I LIKE DISCRETE MATH
Cryptology
Encryption of messages using a shift by 3.
• The encryption of the letter with an index p is represented as:
• f(p) = (p + 3) mod 26
Coding of letters:
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
• Encrypt message:
– I LIKE DISCRETE MATH
7
Cryptology
How to decode the message ?
• The encryption of the letter with an index p is represented as:
• f(p) = (p + 3) mod 26
Coding of letters:
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Representations of Integers
• In the modern world, we use decimal, or base 10, notation to
represent integers. For example when we write 965, we mean
9·102 + 6·101 + 5·100 .
• We can represent numbers using any base b, where b is a
positive integer greater than 1.
• The bases b = 2 (binary), b = 8 (octal) , and b= 16
(hexadecimal) are important for computing and
communications
• The ancient Mayans used base 20 and the ancient Babylonians
used base 60.
M. Hauskrecht
8
Base b Representations
• We can use positive integer b greater than 1 as a base
M. Hauskrecht
Binary Expansions
Most computers represent integers and do arithmetic with
binary (base 2) expansions of integers. In these expansions,
the only digits used are 0 and 1.
Example: What is the decimal expansion of the integer that has
(1 0101 1111)2 as its binary expansion?
Solution:
(1 0101 1111)2 1∙28 0∙27 1∙26 0∙25 1∙24 1∙23
1∙22 1∙21 1∙20 351.
Example: What is the decimal expansion of the integer that has
(11011)2 as its binary expansion?
Solution: (11011)2 1 ∙24 1∙23 0∙22 1∙21 1∙20 27
M. Hauskrecht
9
Octal Expansions
The octal expansion (base 8) uses the digits {0,1,2,3,4,5,6,7}.
Example: What is the decimal expansion of the number with
octal expansion (7016)8 ?
Solution: 7∙83 0∙82 1∙81 6∙80 3598
Example: What is the decimal expansion of the number with
octal expansion (111)8 ?
Solution: 1∙82 1∙81 1∙80 64 8 1 73
M. Hauskrecht
Hexadecimal Expansions
• The hexadecimal expansion uses 16 digits:
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}.
– The letters A through F represent the decimal numbers 10
through 15.
Example: What is the decimal expansion of the number with
hexadecimal expansion (2AE0B)16 ?
Solution:
2∙164 10∙163 14∙162 0∙161 11∙160 175627
Example: What is the decimal expansion of the number with
hexadecimal expansion (E5)16 ?
Solution: 14∙161 5∙160 224 5 229
M. Hauskrecht
10
Base Conversion
To construct the base b expansion of an integer n:
– Divide n by b to obtain a quotient and remainder.
n = bq0 + a0 0 a0 b
– The remainder, a0 , is the rightmost digit in the base b
expansion of n. Next, divide q0 by b.
q0 = bq1 + a1 0 a1 b
– The remainder, a1, is the second digit from the right in the
base b expansion of n.
– Continue by successively dividing the quotients by b,
obtaining the additional base b digits as the remainder. The
process terminates when the quotient is 0.
M. Hauskrecht
Base Conversion
Example: Find the octal expansion of (12345)10
Solution: Successively dividing by 8 gives:
– 12345 = 8 · 1543 + 1
– 1543 = 8 · 192 + 7
– 192 = 8 · 24 + 0
– 24 = 8 · 3 + 0
– 3 =8·0+3
The remainders are the digits from right to left yielding
(30071)8.
M. Hauskrecht
11