Wyższa Szkoła Zarządzania i Bankowości
LECTURESNOTEBOOKSPACKAGES
5 Modeling with Random Numbers
5.2 Generating Random Numbers with Mathematica

1 Introduction to Simulation and Modeling
2 Discrete Medeling (L-Systems)
3 Population Dynamics
4 Number Representation and Error Propagation
5 Modeling with Random Numbers
5.1 What is a Random Number?
5.2 Generating Random Numbers with Mathematica
5.3 Seeds and Probability Distributions
5.4 Integration by Random Numbers and Numerical Methods
5.5 A Drunken Man's Walk
6 Heat Transfer in a Rod (Connection Mathematica and C: MathLink)
7 Special Topics in Stochastic Finance
8 Appendix: Introduction to Mathematica
9 Population Dynamics in Vensim®PLE
     
 

V.2.1 The Random Function

Mathematica can be used to create random numbers with arbitrary probability distributions. The numbers can be integer, real, or complex. There are dozens of different distributions that you can use, or you can create your own.

First we demonstrate a series of random number generations. Here is a random integer between 1 and 100.

[Graphics:Images/nb5_gr_16.gif]
[Graphics:Images/nb5_gr_17.gif]

Here is a random integer in the range from -100 to 100.

[Graphics:Images/nb5_gr_18.gif]
[Graphics:Images/nb5_gr_19.gif]

By omitting the range, the default of 0 to 1 is used. Here are 10 random integers between 0 and 1.

[Graphics:Images/nb5_gr_20.gif]
[Graphics:Images/nb5_gr_21.gif]

Here is a real random number in the range from 1.1 to 5.2.

[Graphics:Images/nb5_gr_22.gif]
[Graphics:Images/nb5_gr_23.gif]

By omitting the range, you get a random number in the default range of 0 to 1.

[Graphics:Images/nb5_gr_24.gif]
[Graphics:Images/nb5_gr_25.gif]

In fact, by omitting the number type, we see that the default is for a real number in the range from 0 to 1.

[Graphics:Images/nb5_gr_26.gif]
[Graphics:Images/nb5_gr_27.gif]

V.2.2 Testing a random number generator

There are numerous ways to look at and test the randomness of a random number generator. A rather intuitive method might look at the distribution of the numbers over the range from which they are taken.

Here is a list of 1000 integers all taken between 0 and 9.

[Graphics:Images/nb5_gr_28.gif]

The two intuitive tests of randomness that we might be concerned with are the frequency with which each digit occurs, and the presence (or lack thereof) of any patterns in the sequence itself. If the above sequence contained approximately equal amounts of each digit but started off like the following sequence, we would be highly suspect.

0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,...

The frequency with which each digit occurs in the list data, can be checked as follows:

[Graphics:Images/nb5_gr_29.gif]
[Graphics:Images/nb5_gr_30.gif]
[Graphics:Images/nb5_gr_31.gif]

ADVANCED
V.2.2a
  Calculate the frequencies of the 'data' from above: Is this what you expected? Hint: perform your experiments as a function of the length of the sequence.

For a more careful analysis of random sequences, a variety of tests are available. One of the more commonly known tests for determining the effective randomness of a sequence, is the χ[Graphics:Images/nb5_gr_32.gif] (chi-squared) test. This test determines how evenly spread the numbers appear in a sequence and uses their frequency of occurrence to come up with a statistic. If [Graphics:Images/nb5_gr_33.gif] is the number of values in the list of a sequence of [Graphics:Images/nb5_gr_34.gif] positive numbers, then for a uniform distribution, we would expect [Graphics:Images/nb5_gr_35.gif] copies of each number. The χ[Graphics:Images/nb5_gr_36.gif] test is given by the following formula:
[Graphics:Images/nb5_gr_37.gif]

Note that [Graphics:Images/nb5_gr_38.gif] sums all the deviations from the expected values.
In the formula, [Graphics:Images/nb5_gr_39.gif] is the frequency of copies of [Graphics:Images/nb5_gr_40.gif] in the sequence. If the statistic is close to the number of values of the bins [Graphics:Images/nb5_gr_41.gif], then the numbers are assumed to be reasonably random. Here is an implementation of the χ[Graphics:Images/nb5_gr_42.gif]  statistic.

[Graphics:Images/nb5_gr_43.gif]

In general, sequences of positive integers are considered sufficiently random if the χ[Graphics:Images/nb5_gr_44.gif] statistic is given by the following table with degree of freedom = n-1: The confidence level of 95% depends on degrees of freedom  dof = n-1 instead of n: when the computed chisquare does not exceed the confidence level listed below, the sequence is sufficiently random.

[Graphics:Images/nb5_gr_45.gif] [Graphics:Images/nb5_gr_46.gif]
1 3.841`
2 5.991`
3 7.815`
4 9.488`
5 11.071`
6 12.592`
7 14.067`
8 15.507`
9 16.919`
10 18.307`


 
     
  Lectures | Notebooks | Packages

 
  Copyright © 2003 Wyższa Szkoła Zarządzania i Bankowości. Wszystkie prawa zastrzeżone
webmaster@wszib.edu.pl