Title: Games, Random Numbers and Introduction to simple statistics
1Games, Random NumbersandIntroduction to simple
statistics
??? tsaiwn_at_csie.nctu.edu.tw
- PRNG
- Pseudo Random Number Generator
2Agenda
- What is random number(??) ?
- How the random numbers generated ?
- rand( ) in C languages Linear Congruential
- Why call Pseudo random ? (P???)
- How to do true random ?
- Application of Rrandom number ?
- Other topics related to Random numbers
- Introduction to simple statistics (????)
3BATNUM game
- http//www.atariarchives.org/basicgames/showpage.p
hp?page14 - An ancient game of two players
- One pile of match sticks (or stones)
- Takes turn to remove 1, maxTake
- (??? 1, ??? maxTake)
- ???????????? !
- Winning strategy ??
Games ??? Random Number! Why?
4Bulls and Cows Game
- http//5ko.free.fr/en/bk.html
- http//en.wikipedia.org/wiki/Bulls_and_cowsht
tp//zh.wikipedia.org/zh-hant/E78C9CE695B0E
5AD97http//boardgames.about.com/od/paperpencil
/a/bulls_and_cows.htmhttp//pyva.net/eng/play/bk.
html http//www.bullscows.com/index.phphttp//ww
w.funmin.com/online-games/bulls-and-cows/index.php
Games ??? Random Number! Why?
5NIM Game
- http//en.wikipedia.org/wiki/Nim
- Nim is a two-player mathematical game of strategy
in which players take turns removing objects from
distinct heaps. On each turn, a player must
remove at least one object, and may remove any
number of objects provided they all come from the
same heap. - ???????????? !
- Winning strategy ??
Games ??? Random Number! Why?
6What is random number ?
- Sequence of independent random numbers with a
specified distribution such as uniform
distribution (equally probable) - Actually, the sequence generated is not random,
but it appears to be. Sequences generated in a
deterministic way are usually called
Pseudo-Random sequences.
?? http//www.gnu.org/software/gsl/manual/gsl-re
f_19.html
Normal distribution? exponential, gamma, Poisson,
7Turbo C ? rand( )?srand( )
Pseudo random number
ltstdlib.hgt
-
- define RAND_MAX 0x7fffu
- static unsigned long seed0
- int rand( )
- seed seed 1103515245 12345
- return seed (RAND_MAX1)
-
- void srand(int newseed)
- seed newseed
-
static global ?????KR??4.6?
??15? 1?binary
static ???file??function ???? seed
?? C ??? rand( ) ????? Normal Distribution!
8Unix ? gcc ? rand( )?srand( )
Pseudo random number
ltstdlib.hgt
-
- define RAND_MAX 0x7fffffffu
- static unsigned long seed0
- int rand( )
- seed seed 1103515245 12345
- return seed (RAND_MAX1)
-
- void srand(int newseed)
- seed newseed
-
static global ?????KR??4.6?
??31? 1?binary
Pseudo random number
?? Dev-Cpp ? gcc ???? 16 bits!
?? C ??? rand( ) ????? Normal Distribution!
9Random Number Generating Algorithms
- Linear Congruential Generators
- Simple way to generate pseudo-random numbers
- Easily cracked
- Produce finite sequences of numbers
- Each number is tied to the others
- Some sequences of numbers will not ever be
generated - Cryptographic random number generators
- Entropy sensors (i.e., extracted randomness)
10Linear Congruential Generator (LCG) for Uniform
Random Digits
- Preferred method begin with a seed, x0, and
successively generate the next pseudo-random
number by xi1 (axi c) mod m, for i
0,1,2, where - m is the largest prime less than largest integer
computer can store - a is relatively prime to m
- c is arbitrary
- Let A be largest integer less than A
(????????), - then N mod m N N/TT
- Accept LCG with m, a, and c which passes tests
which are also passed by know uniform digits
mod ?C/C/Java ?
11The use of random numbers
1. Simulation 2. Recreation (game programming) 3.
Sampling 4. Numerical analysis 5. Decision making
randomness an essential part of optimal
strategies ( in the game theory) 6. Game
program, . . .
12Uniform Distribution(???? )
13Normal Distribution (???? )
14Standard Normal Distribution(??????)
15????(the Normal Distribution)
-
- ?????,???????????????????????,?????????????????,?
??????????????????? - ? X ???????,?? X N(?,?2)?
- ???? ? ???,?2 ?????
?????????????(median)????(mode)????
?? C ??? rand( ) ????? Normal Distribution!
16Central Limit Theorem (CLT)(?????? )
- ??????? n ??,? n ??????????(independent and
identically distributed, I.I.D.)?????(Random
variable)????????????
????n?30?, ????????
17??? C ??????????
- include ltstdlib.hgt
- double randNormal( ) // ?????????
- int i
- double ans 0.0
- for(i1 ilt12 i) ans ans rand(
)/(1.0RAND_MAX)return ans - 6.0 // N(0,
1)
???? N(x, std2) ?
18Summary
- Pseudo-Random Number Generators(PRNG) depend
solely on a seed, which determines the entire
sequence of numbers returned. - How to get true random ? ? change random seed
- How random is the seed?
- Process ID, UserID Bad Idea !
- Current time srand( time(0) ) // good
- If you use the time, maybe I can guess which
seed you used (microsecond part might be
difficult to guess, but is limited)
19Introduction to simple Statistics
- ???
- tsaiwn_at_csie.nctu.edu.tw
20???? ???
- ????????????96???????????????,????????????????????
? - ???????????????,????????,?????????????????????????
?????????
??????? 100???????? "??" ?
212010?????????
- ????????????????,?????????????????,?????50???,?
????32?????????????????????????????,???1960??
???? - ?????????,???????????,??????????????,????11?23?
??,????1273?,?95??????,??? 2.75?????
Sampling ??
222005?????????
??????????????????
??????????????????,????????,???????1103???,?95???
???,?????2.95???
? ?????????
??????, ????????????
232009????????
??????????/??????
?????????????????????,???????????????????????????
?????2009?11?10??11?????,?????932???????????,
??262?????????????????,???????3.2?????????????????
?????????????,???????????
242008???? ????
??????????????????????????????? ???????20??????
???,??????,?????2008?1?12???????,1?13??16???6??1
0???,??1054?????,?95??????,?????3?
252006?10??????????
???????2006/9/27?9/28,????????????,????1112???????
????????????????????,?????2.9???
262005??????????
- ??TVBS?11?21?22????????,?????????????????48,?????
????????27????? - ??????????,??????????????????(BLOG)???,?????????
??,??2????,???????4????? - ?????TVBS?????11?21??22??,?????1033?20????????,?95
?????,????????3.0?????
271936 Presidential Election and Poll
28??1936???????
- ??????????????????????????????
- ??????????????
- ??????,?1929??1933?????????????
- ??????????????????The spender must go?
- ???????????????? (deficit financing)????Balance
the budget of the American people first? - ????????????????????
- ???Literary Digest????????57?43?????
- ?????????????????????
- ????1916??,????????????????
- ????????62?38?????????
- ?????-???-??
- ??Literary Digest??????????????,????????,???????56
?44????? - ?????????????,??????????56?44?????
29Literary Digest???????
- ?????????????,????????,????????????????????
- ????????????????,????????
- ???????
- ????Literary Digest??????????????,???????????????
- ??????????
- ????????,?????????????,???20???,????????????(Land
on),????????????????????
???????????
30Sample size vs. error of estimation
- When we use to construct a 95 confidence
interval for ?, the bound on error of estimation
is B - n
- The estimated standard deviation of p is
-
31???????????
- 1-? Confidence Interval
- B the bound on error of estimation
- Using a conservative value of ? 0.5 in the
formula for required sample size gives - n ?(1-?) 0.5(1-0.5)
1067.11 - Thus, n would need to be 1068 in order to
estimate ? to within .03 with 95 confidence.
95??????,???????3???????
32Consider this program
- ?????????????????,??????????????
- ??????????
- n ?????????
- Average ????
- STD ? n ???????
?? ????????, ???????????????????, ??????????????!
33Descriptive Statistics
- Dispersion
- Range
- Standard deviation
- Variance
- N
- Not P (inferential stats)
- Distribution
- frequency distribution
- Histogram (???)
- Central tendency
- Mean
- Median (???)
- mode (??)
Dispersion ???????
Distribution ????? ??
Central tendency ???????
34Statistics
- Parameters (??????)
- Mean (???) - the average of the data
- Median (???) - the value of middle observation
- Mode (??) - the value with greatest frequency
- Standard Deviation (???) - measure of average
deviation - Variance (???) - the square of standard deviation
- Range (??) - ?? Max(B2B60) Min(B2B60)?
35Mean and Variance
Population Mean / Sample Mean
Sample Variance
36Standard Deviation
- Variance describes the spread (variation) of that
data around the mean. - Sample variance describes the variation of the
estimates. - Standard deviation s is the square root of s2
????? sqrt (???) ??????????
37Compute Variance without mean
Variance (??? ????/n) / n
From Wikipedia.org
38The Central Limit Theorem
- The probability distribution of sample means
is a normal distribution - If infinite number of samples with n gt 30
observations are drawn from the same population
where X ??(µ,s), then
39 Central Limit Theorem (??????)
- For a population with a mean and a variance
, the sampling distribution of the means of
all possible samples of size n generated from the
population will be approximately normally
distributed - with the mean of the sampling
distribution equal to and the variance equal
to assuming that the sample size is
sufficiently large.
40The Normal Distribution
- Described by
- (mean)
- (standard deviation ???)
- Variance ??? ??????
- Write as N( , ) ? N( , 2)
- Area under the curve is equal to 1
- Standard Normal Distribution
-
41Why is the Normal Distribution important?
- It can be a good mathematical model for some
distributions of real data - ACT Scores
- Repeated careful measurements of the same
quantity - It is a good approximation for different types of
chance outcomes (like tossing a coin) - It is very useful distribution to use to model
roughly symmetric distributions - Many statistical inference procedures are based
on the normal distribution - Sampling Distributions are roughly normal (TBC)
42Normal Distributions and the Standard Deviation
Normal Distribution Black line - Mean Red lines -
1 Std. Dev. from the mean (68.26
Interval) Green lines 2 Std. Dev. from the mean
(95.44 Interval) What about 3 Std. Dev. from
the mean?
95 Confidence interval 1.96 Std. Dev.
4368-95-99.7 Rule for Normal Curves
68.26 of the observations fall within ? of the
mean ?
95.44 of the observations fall within 2? of the
mean ?
99.74 of the observations fall within 3? of the
mean ?
44Notations
- It is important to distinguish between empirical
and theoretical distributions - Different notation for each distribution
45Density function of Normal Distribution
- The exact density curve for a particular normal
distribution is described by giving its mean (?)
and its standard deviation (?) - density at x f(x)
46Confidence Intervals (CI) for µ,from a single
sample mean
47Confidence Interval? (1/2)
- ???????????????,??????(random number)?????,???????
??????X1,????X2,???n??,??X1?X2...Xn?n???,?n???????
,?????????? ???,????n????????,????????????? - ????????????????????(sample mean)??
- ????????????Confidence Interval
(????)?????(significance level)?
48Confidence Interval? (2/2)
- ????,?????????????????????????????,????????
- ??????????????????(probability bound)?????????????
????c1????????c2,??????????1 a
,????????????????µ(sample mean)???c1?c2?????? - Probability c1 lt µ lt c2 1 a
???(c1, c2)??????????(confidence
interval) a??????(significance
level) 100(1-a)??????(confidence
level),?????? 1-a??????(confidence coefficient)?
49??????????????????
- ????16??????393??????????????,
- ???1033???????
- ????????,???????????????????,?????????????????,???
??16??????????????(????)??? - ?????????,?????????????????
50Hypothesis Testing?????
- The null hypothesis for the test is that all
population means (level means) are the same.
(H0) - The alternative hypothesis is that one or more
population means differ from the others. (H1)
51PRNG ????
- ?? http//gogle.com ? PRNG ??
- ANSI X9.17 PRNG
- (PRNG Pseudo Random Number Generator)
- Von Neumann ??? middle square method
- Von Neumann architecture ?
- PRNG in RC4 (RC4?? 802.11 ???????)
- http//www.rsa.com
- http//www.wisdom.weizmann.ac.il/itsik/RC4/rc4.ht
ml - WEP RC4 Stream cipher
52ANSI X9.17 PRNG
- Use 3DES and a key K
- Ti Ek(current timestamp)
- outputi Ek(Ti ? seedi)
- seedi1 Ek(Ti ? outputi)
- Weaknesses
- Only 64 bits are used for Ti
- seedi1 can be easily predicted if state
compromise
53Jon von Neumann 1946 suggested the production of
random number using arithmetic operations of a
computer, "middle square", square a
previousrandom number and extract the middle
digits, Example generate 10-digit numbers,
was 5772156649, square 33317792380594909201t
he next number is 7923805949
Middle square
"middle square" has proved to be a comparatively
poor source of random numbers. If zero appear as
a number of the sequence, it will continually
perpetuate itself.
54Von Neumann architecture (http//wikipedia.org/)
- The term von Neumann architecture refers to a
computer design model that uses a single storage
structure to hold both programs and data. The
term von Neumann machine can be used to describe
such a computer, but that term has other meanings
as well. The separation of storage from the
processing unit is implicit in the von Neumann
architecture. - The term "stored-program computer" is generally
used to mean a computer of this design.
Von Neumann bottle neck ?
55Seeding RC4
RC4 PRNG (1/2)
- for(I 0 I lt 256 I)
- SI I
- for (I J 0 I lt 256 I)
- j SI KI klen
- SWAP(SI, SJ)
-
- I J 0
56RC4 PRNG (2/2)
- rc4byte()
-
- I
- J SI
- SWAP(SI, SJ)
- return (S SI SJ )
Byte version
57WEP RC4 ??? (http//rsa.com)
Random bit stream b
Plaintext bit stream p
Ciphertext bit stream c
?
XOR
Decryption works in the same way p c ? b
WEP Wired Equivalent Privacy
58Games, Random NumbersandIntroduction to simple
statistics
- ????
- http//www.csie.nctu.edu.tw/tsaiwn/introcs/
- http//gogle.com/