---
title: "Random number properties; KS test algorithm"
subject: "Simulation and Modeling"
chapter: 6
key: "semester_8/simulation_and_modeling:rn-properties-ks-algorithm"
source: https://www.bctengineer.com/q/semester_8/simulation_and_modeling/rn-properties-ks-algorithm
site: BCT Engineering
---

# Random number properties; KS test algorithm

Simulation and Modeling · Chapter 6: Random Number

- Asked in: 2073 Bhadra, 4+6 marks
- Video: [Simulation Modeling 29 Hypothesis Testing for Uniformity and Independence](https://www.youtube.com/watch?v=FZZ27Y1ctqs) — İsmail Başoğlu
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_8/simulation_and_modeling/rn-properties-ks-algorithm
- Whole chapter: https://www.bctengineer.com/md/semester_8/simulation_and_modeling/chapter-6

## Question

What are the properties of random numbers? Explain the algorithm of Kolmogorov-Smirnov Test with example.

## Short answer

Random numbers must be uniformly distributed on [0,1), independent, and have maximum density, long period, reproducibility, and efficiency. The K-S test ranks N numbers and computes D = max(i/N − R(i), R(i) − (i−1)/N); if D ≤ Dα uniformity is accepted. Example numbers 0.05,0.14,0.44,0.81,0.93 gave D=0.26 < 0.565, so uniform.

## Answer

#### Properties of random numbers
Random numbers used in simulation are uniformly distributed on [0, 1) and statistically independent.

1. **Uniformity.** Each number is equally likely to fall in any sub-interval of [0, 1). If the interval is divided into n equal classes, each class should receive about N/n of the N numbers. The density is f(R) = 1 for 0 ≤ R ≤ 1, so E(R) = 1/2 and Var(R) = 1/12.
2. **Independence.** The value of one number must not depend on any earlier or later number. There should be no pattern, trend or correlation in the sequence, so knowing R1 tells nothing about R2.
3. **Maximum density.** Values should cover [0, 1) closely, so the generator should have a very large number of distinct values (small gaps between possible values).
4. **Long (maximum) period.** The sequence should not repeat for a very long time. Ideally the period is as large as the modulus m (full period).
5. **Reproducibility.** The same seed must give the same sequence. This is needed for debugging and for comparing alternative designs with the same random inputs.
6. **Efficiency and portability.** The generator should be fast, need little memory, and give the same results on different computers.

#### Kolmogorov-Smirnov (K-S) test
The K-S test compares the empirical (sample) cdf SN(x) of the generated numbers with the cdf of the uniform distribution, F(x) = x for 0 ≤ x ≤ 1. It works on the individual numbers, so it needs no grouping into classes and suits small samples.

**Hypotheses:** H0: the numbers are uniformly distributed on [0, 1]. H1: they are not.

**Algorithm**
1. Rank the N numbers from smallest to largest: R(1) ≤ R(2) ≤ ... ≤ R(N).
2. Compute D⁺ = max over i of [ i/N − R(i) ].
3. Compute D⁻ = max over i of [ R(i) − (i−1)/N ].
4. Compute D = max(D⁺, D⁻).
5. Find the critical value Dα from the K-S table for the chosen significance level α and sample size N.
6. If D > Dα, reject H0 (not uniform). If D ≤ Dα, no evidence to reject H0, so accept uniformity.

**Example.** Test the five numbers 0.44, 0.81, 0.14, 0.05, 0.93 with α = 0.05.

Ranked values and the differences:

| i | R(i) | i/N | i/N − R(i) | R(i) − (i−1)/N |
|---|---|---|---|---|
| 1 | 0.05 | 0.20 | 0.15 | 0.05 |
| 2 | 0.14 | 0.40 | 0.26 | -0.06 |
| 3 | 0.44 | 0.60 | 0.16 | 0.04 |
| 4 | 0.81 | 0.80 | -0.01 | 0.21 |
| 5 | 0.93 | 1.00 | 0.07 | 0.13 |

D⁺ = 0.26, D⁻ = 0.21, so **D = max(0.26, 0.21) = 0.26**.

Critical value for N = 5, α = 0.05 is D0.05 = 0.565.

Since 0.26 < 0.565, H0 is not rejected. No difference between the sample and the uniform distribution has been detected, so the numbers can be used as uniform random numbers.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. For R uniform on [0,1], what are E(R) and Var(R)?
   A. 1/2 and 1/12
   B. 1/2 and 1/2
   C. 1/12 and 1/2
   D. 1 and 1/12
   Answer: A. With f(R) = 1 on [0,1], E(R) = 1/2 and Var(R) = 1/12. (Key point: Uniform mean and variance)

2. What does the K-S test compare?
   A. Observed class counts with expected class counts
   B. The empirical cdf SN(x) with the uniform cdf F(x) = x
   C. Gap lengths with a geometric law
   D. Runs up with runs down
   Answer: B. K-S compares the sample cdf SN(x) with the uniform cdf, using individual numbers without grouping. (Key point: K-S compares cdfs)

3. How are D+ and D- computed from ranked numbers R(i)?
   A. D+ = max[R(i) - i/N], D- = max[(i-1)/N - R(i)]
   B. D+ = max[i/N - R(i)], D- = max[R(i) - (i-1)/N]
   C. D+ = min[i/N - R(i)], D- = min[R(i) - (i-1)/N]
   D. D+ = sum of i/N - R(i), D- = sum of R(i)
   Answer: B. D+ = max(i/N - R(i)) and D- = max(R(i) - (i-1)/N); D is the larger of the two. (Key point: D+ and D-)

4. In the example D = 0.26 and D0.05 = 0.565 for N = 5. What is the decision?
   A. Reject H0 because D is large
   B. Reject H0 because N is small
   C. Do not reject H0 since D < critical value
   D. No decision can be made
   Answer: C. Since 0.26 < 0.565, there is no evidence against uniformity, so H0 is not rejected. (Key point: K-S decision)

## Related in Simulation and Modeling

- [Pseudo-random; Kolmogorov-Smirnov numerical](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Aks-test-uniform-numerical) — Simulation and Modeling, chapter 6
- [Random number properties and Gap test](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Arn-properties-gap-test) — Simulation and Modeling, chapter 6, asked 4×
- [Properties of random numbers](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Arn-properties) — Simulation and Modeling, chapter 6
- [Chi-square test on 100 random numbers](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Achi-square-uniformity-100-numbers) — Simulation and Modeling, chapter 6
- [Random number; problems in generating pseudo-random](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Arn-problems-pseudo-random) — Simulation and Modeling, chapter 6
- [Chi-square uniformity test, 40 numbers](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Achi-square-uniformity-table-2074) — Simulation and Modeling, chapter 6

## Related in other subjects

- [Algorithm for numbers divisible by 9 not 6](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-divisible-9-not-6) — Computer Programming, chapter 2
