Implementation / Programming: Random Number Generation

Similar documents
Solutions to Problem Set 6 - Fall 2008 Due Tuesday, Oct. 21 at 1:00

Discrete Square Root. Çetin Kaya Koç Winter / 11

Collection of rules, techniques and theorems for solving polynomial congruences 11 April 2012 at 22:02

Applications of Fermat s Little Theorem and Congruences

SOLUTIONS TO PROBLEM SET 5. Section 9.1

ALGEBRA: Chapter I: QUESTION BANK

Solutions for the 2nd Practice Midterm

MATH 324 Elementary Number Theory Solutions to Practice Problems for Final Examination Monday August 8, 2005

Number Theory - Divisibility Number Theory - Congruences. Number Theory. June 23, Number Theory

6. Find an inverse of a modulo m for each of these pairs of relatively prime integers using the method

The congruence relation has many similarities to equality. The following theorem says that congruence, like equality, is an equivalence relation.

b) Find all positive integers smaller than 200 which leave remainder 1, 3, 4 upon division by 3, 5, 7 respectively.

Discrete Math Class 4 ( )

Modular Arithmetic. Kieran Cooney - February 18, 2016

Introduction. and Z r1 Z rn. This lecture aims to provide techniques. CRT during the decription process in RSA is explained.

p 1 MAX(a,b) + MIN(a,b) = a+b n m means that m is a an integer multiple of n. Greatest Common Divisor: We say that n divides m.

1 = 3 2 = 3 ( ) = = = 33( ) 98 = = =

A4M33PAL, ZS , FEL ČVUT

Foundations of Cryptography

NUMBER THEORY AMIN WITNO

Distribution of Primes

Class 8: Square Roots & Cube Roots (Lecture Notes)

Multiples and Divisibility

Solutions for the Practice Questions

Linear Congruences. The solutions to a linear congruence ax b (mod m) are all integers x that satisfy the congruence.

Quantitative Aptitude Preparation Numbers. Prepared by: MS. RUPAL PATEL Assistant Professor CMPICA, CHARUSAT

Calculators will not be permitted on the exam. The numbers on the exam will be suitable for calculating by hand.

Modular Arithmetic. claserken. July 2016

Fermat s little theorem. RSA.

To be able to determine the quadratic character of an arbitrary number mod p (p an odd prime), we. The first (and most delicate) case concerns 2

Introduction to Modular Arithmetic

Number Theory/Cryptography (part 1 of CSC 282)

LECTURE 3: CONGRUENCES. 1. Basic properties of congruences We begin by introducing some definitions and elementary properties.

Solutions to Exam 1. Problem 1. a) State Fermat s Little Theorem and Euler s Theorem. b) Let m, n be relatively prime positive integers.

2. Nine points are distributed around a circle in such a way that when all ( )

Math 255 Spring 2017 Solving x 2 a (mod n)

Modular arithmetic Math 2320

Multiple : The product of a given whole number and another whole number. For example, some multiples of 3 are 3, 6, 9, and 12.

Solutions for the Practice Final

Section 5.4. Greatest Common Factor and Least Common Multiple. Solution. Greatest Common Factor and Least Common Multiple

Pseudorandom Number Generation and Stream Ciphers

Number Theory and Public Key Cryptography Kathryn Sommers

Adding Fractions with Different Denominators. Subtracting Fractions with Different Denominators

Constructions of Coverings of the Integers: Exploring an Erdős Problem

Power = 36² mod 99 Power = 9 5 a 5 = 0 x = 81 Power = 9² mod 99 Power = 81 6 a 6 = 1 x = 81 x 81 mod 99 x = 27 7 a 7 = 1 x = 27 x 27 mod 99 x = 36

Number Theory. Konkreetne Matemaatika

UNIVERSITY OF MANITOBA DATE: December 7, FINAL EXAMINATION TITLE PAGE TIME: 3 hours EXAMINER: M. Davidson

Numbers (8A) Young Won Lim 6/21/17

Data security (Cryptography) exercise book

Numbers (8A) Young Won Lim 5/24/17

Class 8: Factors and Multiples (Lecture Notes)

Carmen s Core Concepts (Math 135)

Degree project NUMBER OF PERIODIC POINTS OF CONGRUENTIAL MONOMIAL DYNAMICAL SYSTEMS

Lecture 8. Outline. 1. Modular Arithmetic. Clock Math!!! 2. Inverses for Modular Arithmetic: Greatest Common Divisor. 3. Euclid s GCD Algorithm

Mathematics Explorers Club Fall 2012 Number Theory and Cryptography

DUBLIN CITY UNIVERSITY

Exam 1 7 = = 49 2 ( ) = = 7 ( ) =

1.6 Congruence Modulo m

SMT 2014 Advanced Topics Test Solutions February 15, 2014

Random Bit Generation and Stream Ciphers

Math 412: Number Theory Lecture 6: congruence system and

A C E. Answers Investigation 3. Applications = 0.42 = = = = ,440 = = 42

Example Enemy agents are trying to invent a new type of cipher. They decide on the following encryption scheme: Plaintext converts to Ciphertext

Public Key Cryptography Great Ideas in Theoretical Computer Science Saarland University, Summer 2014

Numbers (8A) Young Won Lim 5/22/17

Final Exam, Math 6105

DUBLIN CITY UNIVERSITY

The Chinese Remainder Theorem

Squares and Square roots

The Chinese Remainder Theorem

#A3 INTEGERS 17 (2017) A NEW CONSTRAINT ON PERFECT CUBOIDS. Thomas A. Plick

Modular Arithmetic: refresher.

University of British Columbia. Math 312, Midterm, 6th of June 2017

Primitive Roots. Chapter Orders and Primitive Roots

CMPSCI 250: Introduction to Computation. Lecture #14: The Chinese Remainder Theorem David Mix Barrington 4 October 2013

Math 1111 Math Exam Study Guide

8.2 Union, Intersection, and Complement of Events; Odds

Math 127: Equivalence Relations

Practice Midterm 2 Solutions

Diffie-Hellman key-exchange protocol

PT. Primarity Tests Given an natural number n, we want to determine if n is a prime number.

MATH 135 Algebra, Solutions to Assignment 7

Cryptography, Number Theory, and RSA

An ordered collection of counters in rows or columns, showing multiplication facts.

It is important that you show your work. The total value of this test is 220 points.

PRMO Official Test / Solutions

Sheet 1: Introduction to prime numbers.

Public Key Cryptography

CHAPTER 2. Modular Arithmetic

6.2 Modular Arithmetic

ORDER AND CHAOS. Carl Pomerance, Dartmouth College Hanover, New Hampshire, USA

MODULAR ARITHMETIC II: CONGRUENCES AND DIVISION

Modular Arithmetic and Doomsday

Course Syllabus - Online Prealgebra

Assignment 2. Due: Monday Oct. 15, :59pm

Chapter 4 Cyclotomic Cosets, the Mattson Solomon Polynomial, Idempotents and Cyclic Codes

SOLUTIONS FOR PROBLEM SET 4

Mathematical Olympiads November 19, 2014

Math 1111 Math Exam Study Guide

Asst. Prof. Thavatchai Tayjasanant, PhD. Power System Research Lab 12 th Floor, Building 4 Tel: (02)

Transcription:

Introduction to Modeling and Simulation Implementation / Programming: Random Number Generation OSMAN BALCI Professor Department of Computer Science Virginia Polytechnic Institute and State University (Virginia Tech) Blacksburg, VA 24061, USA https://manta.cs.vt.edu/balci Copyright Osman Balci

Basic Concepts in Number Theory Background for Random Number Generation 1. For any pair of integers n and m, m 0, there exists a unique pair of integers such that n = mq + r with 0 r < m, where m is the modulus, q the quotient, and r the residue (remainder). This is commonly written as r n (mod m) and is read as r is congruent to n modulo m. Congruent is defined as having the difference divisible by a given modulus. For example: 12 2 (mod 5) since 12-2 is divisible by 5. 7-1 (mod 8) since 7 + 1 is divisible by 8.

Basic Concepts in Number Theory 2. Commonly, the residue is the remainder after division by m. There are m distinct residues (modulo m): 0, 1, 2,..., m - 1. 3. Two integers a and b are Congruent Modulo m if their difference is an integral multiple of m. For example, let a = 15, b = 8, and m = 7. 1 15 (mod 7) and 1 8 (mod 7) 4. An integer x is a Prime Number if it is neither 0 nor 1 and if its only divisors are 1 and x. Example positive prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,... 5. An integer g is the Greatest Common Divisor (GCD) of two integers a and b if g is a common divisor of a and b and is a multiple of every other common divisor of a and b. This is usually written as (a, b) = g or GCD (a, b) = g. (12, 16) = 4

Basic Concepts in Number Theory 6. The integers a and b are said to be Relatively Prime if (a, b) = 1. 7 and 12 are relatively prime because (7, 12) = 1 7. A Residue Class is a class of all integers which are mutually congruent for a given modulus. There are m distinct residue classes (modulo m), corresponding to the terms of a complete residue system modulo m. Collectively they comprise the class of all integers:... 36 29 22 15 08 1-6 (mod 7)... 37 30 23 16 09 2-5 (mod 7)... 38 31 24 17 10 3-4 (mod 7)... 39 32 25 18 11 4-3 (mod 7)... 40 33 26 19 12 5-2 (mod 7)... 41 34 27 20 13 6-1 (mod 7)... 42 35 28 21 14 7-0 (mod 7)

Basic Concepts in Number Theory 8. A Complete Residue System is a set of m numbers, for a given modulus m, congruent in some order to the residues 0,1,2,...,m-1. A CRS for 7: 4 8 14 9 10 12 13 (mod 7) Residue: 4 1 0 2 3 5 6 9. A subset of a complete residue system, containing all terms which are relatively prime to m, is termed a Reduced Residue System. 4, 8, 9, 10, 12, 13 10. Euler's Phi-function, f(.), denotes the number of positive integers less than m and relatively prime to m, so a reduced residue system contains f(m), terms if m is not a prime, f(m) = m - 1 if m is a prime. f(7) = 7-1 = 6

Basic Concepts in Number Theory 11. Power Residues are the residues of the successive powers of a number (i.e., x n (mod m) for n = I, 2, 3,... ). For x=4 and n = I, 2, 3, 4, 5, 6,... the values 4, 2, 1, 4, 2, 1,... are the power residues (mod 7). 12. Order of x modulo m is defined to be the least positive exponent h with x h 1 (mod m) when x and m are relatively prime. Order of 4 modulo 7 = 3 (4 3 1 (mod 7)) 13. The Primitive Root of m is a number x whose order h is equal to f(m). For the reduced residue system in the example above (4, 8, 9, 10, 12, 13); a primitive root of 7 is 10. Note that 12 is also a primitive root. 4 3 8 1 9 3 10 6 12 6 13 2 1 (mod 7)

Basic Concepts in Number Theory 14. If (x, m) = 1, then x n (mod m), where n = 0, 1, 2, 3,..., repeats by returning to the starting point since x r x s (mod m) implies x r-s 1 (mod m); the division is possible because (x, m) = 1. 15. If a b (mod m) and c d (mod m), then a c b d (mod m). 16. If a b (mod m), then ac bc (mod mc). 17. If a b (mod m) and b c (mod m), then a c (mod m). 18. If a b (mod m) and d is any divisor of m, then a b (mod d). 19. If a b (mod m) and c d (mod m), then ac bd (mod m). 20. If a b (mod m), with d as any common divisor of a and b, and (m, d) = g then (a/d) (b/d) ( mod (m/g)).

Pseudo-Random Number Generation Linear Congruential Generators (LCGs) where a Z 0 c m Z i (a Z i-1 + c ) (mod m) is the multiplier is the seed or starting value is the constant is the modulus a, Z 0, c, and m are positive integers 0 Z i m - 1 for i = 1, 2, 3,... To obtain random numbers on [0, 1], we let U i = Z i / m 0 < m, a < m, c < m, and Z 0 < m Why Z 0 cannot be equal to m?

Linear Congruential Generators The length of a cycle is called the period (p) of a generator. Since 0 Z i m - 1 then p m. If p = m, the LCG is said to have Full Period. Question: How should we choose m, a, and c values so that the corresponding LCG will have full period? Theorem 1: Z i (a Z i-1 + c ) (mod m) has full period (length m) if and only if 1. (c, m) = 1 2. If q is a prime number that divides m, then q divides a - 1 3. If 4 divides m, then 4 divides a - 1

Linear Congruential Generators Theorem 2: Z i a Z i-1 (mod m) has full period if and only if 1. m is a prime number 2. a is a primitive root modulo m If a and m are chosen to satisfy Theorem 2, Z i = 1, 2, 3,..., m - 1 (cycle) Period = m - 1 0 Z 0 m - 1

Mixed Congruential Generators Z i (a Z i-1 + c ) (mod m) Since this LCG's full period is m, we want m as large as possible. If a computer has a word size of b bits, the largest number that can be represented is 2 b-1-1. If b=4, the largest number is 111 = 1 2 2 + 1 2 1 + 1 2 0 = 7 since one bit (leftmost) is used for sign. However, m is chosen to be 2 b-1 to avoid explicit division by m on some computers by taking advantage of integer overflow. Example: Let b=5. Thus we choose 2 4 =16=m when in fact 15 is the largest number that can be represented by a word of 5 bits. Now let s see how we avoid the division by the overflow. Any attempt to store an integer larger than 15 will result in loss of the leftmost bits in overflow.

Mixed Congruential Generators Consider Z i (5 Z i-1 + 3 ) (mod 16) Let Z i-1 = 8. Then 5 8 + 3 = 43. 43 16 = 2 with a remainder of 11. 2 5 2 4 2 3 2 2 2 1 2 0 32 16 8 4 2 1 1 0 1 0 1 1 ) 2 = 43 ) 10 dropped = 11 ) 10 Tested values for a 36-bit computer: Z i (5 15 Z i-1 + 1) (mod 2 35 ) Tested values for a 32-bit computer: Z i (p 10 8 Z i-1 + 453806245) (mod 2 31 )

From Theorem 2: Multiplicative Congruential Generators Z i a Z i-1 (mod m) m must be a prime number, and a must be a primitive root modulo m to achieve the full period of m - 1. The modulus m must be as large as possible; however, we cannot choose m = 2 b-1 since it is divisible by 2 for all values of the word size b. Hence the largest prime number is 2 b-1-1 which is used for m. Tested values for 32-bit computers: Z i 7 5 Z i-1 (mod 2 31-1) E Use this one

Additive Congruential Generators k Z i+1 S d j Z i-j ) (mod m) j=0 Quadratic Congruential Generators Z i a Z 2 i-1 + a Z i-1 + c) (mod m)

Table Shuffling Approach Combinational Techniques 1. Create a one-dimensional array (Table) of K (= 128) elements. 2. Generate K random numbers and fill in the Table using the following LCG 1: U i (2 17 + 3) U i-1 (mod 2 35 ) 3. Generate an integer random number N over [1, K] using the following LCG 2: V i ((2 7 + 1) V i-1 + 1) (mod 2 35 ) 4. Deliver the N th element of the Table as the random number. 5. Replace the N th element of the Table by using the LCG 1 in Step 2.

Combinational Techniques Bit String Perturbation Approach Let Z i (1) and Z i (2) be the values of Z i produced by the first and second LCG's given on the previous slide, respectively. 1. Using the bits of Z i (1), rotate circularly the bits of Z i (2) to obtain Z i (3) between 0 and m - 1. 2. Using a bitwise addition modulo 2 (Exclusive OR = XOR) on the bits of Z i (1) and Z i (3), obtain Z i (4). 3. Deliver U i = Z i (4) / m

TAUSWORTHE GENERATORS b i (c 1 b i-1 + c 2 b i-2 + + c q b i-q ) (mod 2) where C j is a constant 0 or 1 and b k is a bit. The maximum period = 2 q - 1. Usually, only two of the C j 's are nonzero. b i (b i-r + b i-q ) (mod 2) for integers r and q satisfying 0 < r < q. The following Exclusive OR operation is equivalent to the congruent relation given above: b i = 0 if b i-r = b i-q 1 if b i-r b i-q

TAUSWORTHE GENERATORS Example: Let r = 3 and q = 4. b i (b i-3 + b i-4 ) (mod 2) i = 1 1 0 1 1 i = 2 0 1 1 0 i = 3 1 0 0 0 i = 4 0 1 0 0 i = 5 1 1 0 1 i = 6 1 1 1 0 i = 7 1 1 0 0 i = 8 1 0 0 1 i = 9 0 0 1 1 i = 10 0 0 1 0 i = 11 0 1 0 1 i = 12 1 0 1 0 i = 13 0 0 0 1 i = 14 0 1 1 1 b i = i = 15 1 1 1 1 End of cycle i = 16 1 0 1 1 Note that the period is 2 q 1 = 15 Initial Values Assume 4 bits are used to store an integer number Initial starting 4 values must then be given. 0 if b i-r = b i-q 1 if b i-r b i-q

Desired Properties of a Random Number Generator 1. Random numbers must be independent and identically distributed over [0, 1]. 2. Random numbers must be uniformly distributed. 3. Random numbers must be reproducible. 4. Must have high execution speed and minimum amount of storage. 5. Random numbers must have a considerably large period (cycle). 6. The generator should be generally available.

Testing for Randomness 1. Frequency Test: Uses either the Chi-square or Kolmogorov- Smirnoff test to compare the distribution of the set of numbers generated against a uniform distribution. 2. Serial Test: Tallies the frequency of occurrence of all possible combinations of 2, 3, 4, etc., digits and then runs a Chi-square test against expected values. 3. Gap Test: Counts the number of digits that appear between repetitions of a particular digit and then uses a Chi-square test against expected values. 4. Runs Test: Tests the number of runs above and below some constant (usually the mean) or runs up and down. The test involves counting the actual number of occurrences of runs of different lengths and comparing these counts to expected values by Chi-square. 5. Spectral Test: Measures the independence of adjacent sets of n numbers based on Fourier analysis.

Testing for Randomness 6. Poker Test: Analogous to testing poker hands. This test counts combinations of five or more digits for all digits different, one pair, two pairs, three of a kind, full house, etc., and tests against expected occurrences. 7. Autocorrelation Test: Tests the correlation between X n and X n+k where k is the lag in the generation order (k = 1, 2, 3,...). 8. D 2 or Distance Test: Successive pairs of random numbers are regarded as coordinates for points in the unit square, and the square of the distance between the two points is tested against theoretical probabilities given by a set of equations. 9. Order Statistic Test: Tests the maximum or minimum value of n consecutive numbers or the range of n consecutive values. 10. Yule s Test: Consists of taking the sum of five decimal digits from normalized random numbers and comparing it with the theoretical expected values.