Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-049

LLN Running Average (Seeded)

Problem

Law of large numbers demonstration with a seeded simulation.

Simulate n independent Bernoulli(p) trials using random.Random(seed) and return the observed success frequency. Return 0.0 for n <= 0. Decide each trial with rng.random() < p so results are reproducible.

Hint

Seed a private random.Random instance instead of the global module state.

Test cases

case 1
in: [10000,0.5,42]
expected: 0.499
case 2
in: [10000,0.5,7]
expected: 0.503
case 3
in: [1,0.5,0]
expected: 0
case 4
in: [100,0.3,123]
expected: 0.35
Starter codePython
import random


def lln_running_average(n, p, seed):
    # Your code here
    pass

4 test cases · run your code against them in the editor