---
title: "Gap test algorithm; formulate poker test"
subject: "Simulation and Modeling"
chapter: 6
key: "semester_8/simulation_and_modeling:gap-test-poker-formulate"
source: https://www.bctengineer.com/q/semester_8/simulation_and_modeling/gap-test-poker-formulate
site: BCT Engineering
---

# Gap test algorithm; formulate poker test

Simulation and Modeling · Chapter 6: Random Number

- Asked in: 2071 Bhadra, 4+6 marks
- Video: [Simulation Modeling | Tutorial #18 | The Poker's Test (Solved Problem)](https://www.youtube.com/watch?v=1vy480sUxaI) — RANJI RAJ
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_8/simulation_and_modeling/gap-test-poker-formulate
- Whole chapter: https://www.bctengineer.com/md/semester_8/simulation_and_modeling/chapter-6

## Question

Write an algorithm for gap test. Formulate 4-digit poker test with suitable data with example.

## Short answer

Gap test: pick target, count gaps between hits, compute F(x)=1−(1−p)^(x+1), test with K‑S or χ². Poker test: group four‑digit numbers; probabilities: all different 0.504, one pair 0.432, two pair 0.027, three of a kind 0.036, four of a kind 0.001. Compute χ²=∑(Oi−Ei)²/Ei with df=4; example with N=1000 gave χ²=1.069 < 9.488, not reject.

## Answer

#### Algorithm for the gap test
The gap test checks independence by examining the gaps between successive occurrences of a chosen digit. For a single digit the chance of occurrence is p = 0.1 (for an interval [α, β), p = β − α). Gap length x has probability p(1 − p)^x and cdf F(x) = 1 − (1 − p)^(x+1).

1. Choose the target digit or interval [α, β).
2. Scan the sequence and note each position where the target occurs. The gap is the count of non-target numbers between two consecutive occurrences.
3. Group the n gaps by length (0, 1, 2, ..., and a last class "x or more").
4. Find the observed relative frequencies and the cumulative function SN(x).
5. Compute the theoretical F(x) = 1 − (1 − p)^(x+1).
6. Compute D = max|F(x) − SN(x)| (K-S) and compare it with Dα for n gaps. Alternatively use χ0² = Σ (Oi − Ei)²/Ei with Ei = n·p(1 − p)^x.
7. If the statistic is greater than the critical value, reject independence; else accept.

#### Four-digit poker test
The poker test treats each group of four digits like a poker hand and compares the observed counts of each pattern with the expected counts, using the chi-square test. Each random number is taken to four decimal places (e.g. 0.5127 gives digits 5, 1, 2, 7).

**Probabilities of the patterns**

| Pattern | Probability |
|---|---|
| All four different | 0.504 |
| One pair | 0.432 |
| Two pairs | 0.027 |
| Three of a kind | 0.036 |
| Four of a kind | 0.001 |

Probabilities are found by counting: all different = 10·9·8·7/10⁴ = 0.504; four alike = 10/10⁴ = 0.001; and so on. They sum to 1.

**Steps:** (1) Take N numbers and classify each into a pattern. (2) Expected count Ei = N × probability. (3) Compute χ0² = Σ (Oi − Ei)²/Ei. (4) Degrees of freedom = classes − 1 = 4. (5) Reject independence if χ0² > χ²α,4.

**Example.** Suppose N = 1000 four-digit numbers give these counts (data assumed for illustration):

| Pattern | Oi | Ei | (Oi − Ei)²/Ei |
|---|---|---|---|
| All different | 505 | 504 | 0.002 |
| One pair | 425 | 432 | 0.113 |
| Two pairs | 32 | 27 | 0.926 |
| Three of a kind | 37 | 36 | 0.028 |
| Four of a kind | 1 | 1 | 0.000 |

χ0² = 1.069. With α = 0.05 and df = 4, the critical value is 9.488. Since 1.069 < 9.488, we do not reject H0, and the numbers are accepted as independent.

## Check yourself

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

1. For a single target digit, the cdf of the gap length x is:
   A. F(x) = 1 - (0.9)^(x+1)
   B. F(x) = 0.1 x
   C. F(x) = (0.1)^x
   D. F(x) = 1 - 0.1^(x+1)
   Answer: A. With p = 0.1, F(x) = 1 - (1 - p)^(x+1) = 1 - 0.9^(x+1). (Key point: Gap probability)

2. Why is P(all four digits different) = 0.504?
   A. 10 x 9 x 8 x 7 / 10^4
   B. 10 x 4 / 10^4
   C. 4! / 10^4
   D. 9^4 / 10^4
   Answer: A. Counting ordered choices of 4 different digits gives 10 x 9 x 8 x 7 = 5040 out of 10^4 = 0.504. (Key point: All different probability)

3. How many degrees of freedom are used in the four-digit poker test with five patterns?
   A. 5
   B. 3
   C. 4
   D. 10
   Answer: C. df = classes - 1 = 5 - 1 = 4. (Key point: Poker test degrees of freedom)

4. How is the expected count Ei and the statistic computed?
   A. Ei = N x probability; chi-square = sum (Oi - Ei)^2 / Ei
   B. Ei = N / 5; chi-square = sum (Oi - Ei)
   C. Ei = N x probability; chi-square = sum |Oi - Ei|
   D. Ei = probability; chi-square = max (Oi - Ei)
   Answer: A. Ei = N x pattern probability, and chi-square0 = sum (Oi - Ei)^2/Ei. (Key point: Poker test statistic)

## Related in Simulation and Modeling

- [Poker test, 10,000 five-digit numbers (2072)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2072-ashwin) — Simulation and Modeling, chapter 6, asked 4×
- [Poker test, 1000 four-digit numbers (2070 Magh)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2070-magh) — Simulation and Modeling, chapter 6, asked 3×
- [Poker test, 1000 four-digit numbers (2069)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2069-bhadra) — Simulation and Modeling, chapter 6, asked 3×
- [Poker test, 10,000 five-digit numbers (2074)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2074-magh) — Simulation and Modeling, chapter 6, asked 2×
- [Poker test, 1000 four-digit numbers (2072 Magh)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2072-magh) — Simulation and Modeling, chapter 6
- [Poker test, 10,000 four-digit values (2070)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2070-bhadra) — Simulation and Modeling, chapter 6

## Related in other subjects

- [Cast steel ring with air gap, B-H data](https://www.bctengineer.com/question.md?key=semester_4%2Felectrical_machines%3Acast-steel-ring-bh-table) — Electrical Machines, chapter 1
- [Short note: Routing algorithm in data communication](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Arouting-algorithm-data-comm) — Telecommunications, chapter 8, asked 2×
- [Tensile test data of a steel bar](https://www.bctengineer.com/question.md?key=semester_3%2Fmaterial_science%3Atensile-test-numerical-steel-bar) — Material Science, chapter 3
