Download Random number generator

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of the function concept wikipedia , lookup

Karhunen–Loève theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Infinite monkey theorem wikipedia , lookup

Central limit theorem wikipedia , lookup

Law of large numbers wikipedia , lookup

Transcript
Homework 2 (Due 2016.10.05)
Random number generator
A.
For a random number generated by using a MATLAB function
rand(N) which is a uniform distribution with length N,
(1) plot the output data x = rand(N) with respect to the time sequence in
running the function.
(2) plot the histogram of x with 100~more (above5000) data generated by
using the function.
Histogram:
Histogram counts the number of elements within a range and display
each range as a rectangular bin. The height of the bins represents the
number of values that fall within each range.
(Ref: http://en.wikipedia.org/wiki/Histogram)
The true mean of the data generated by the function is 0.5. By
assigning a “head”, and “tail” for the random number which is greater,
and less than 0.5, respectively, simulate the behavior of tossing a coin by
finding the probability of head or tail in a consecutive N-times tossing
test. Plot the results of probability of head or tail with respect to different
numbers of test in the tossing experiment.
P.S. You can try more data in this tossing experiment for better
conclusive results.
B.
Random number generator (RNG) implemented by software is
actually to generate a stream of pseudo random number. The most widely
used RNGs for uniform distribution are based on linear recurrences of the
form
xi = (a1xi−1 + … + akxi−k) mod m,
(1)
where the modulus m and the order k of the recurrence are positive
integers, the coefficients al belong to Zm = {0, 1, … ,m-1}, and the state at
step i is si = (xi−k+1, … ,xi). If m is a prime number and if the ai’s satisfy
certain conditions, the sequence {xi , i ≧ 0} has the maximal period
length ρ= mk – 1.
One way of defining the output function, in the case where m is large,
is simply to take.
ui = xi /m.
(2)
The resulting RNG is known under the name of multiple recursive
generator (MRG). When k = 1, we obtain the classical linear congruential
generator (LCG).
Some generators for 32-bits machine with m=231-1 and k=1, 2 are:
231-1
231-1
231-1
231-1
m
1
1
2
2
k
1385320287
41358
1498809829
46325
a1
X
X
1160990996
1084587
a2
The xi-1 to xi-k in the RNG must be initialized to value in the range
[0,231-2], with at least one nonzero value. The initial values are called
seeds. Whenever you are running repeatedly with the same seeds and
algorithm, the stream of random number will repeat itself starting from
the same seeds.
(1) Write your own program of linear recurrent RNG and compare your
results with rand() in MATLAB by using time series and histogram.
(2) What is the algorithm implemented in the rand()?
(3) A Pseudo Random Number Generator will repeat generated random
number under same initial starting condition. Verify your results.
(4) Discuss on your simulated results.
MATLAB function: rand(),hist(),mod()