William is interested in numbers. His friend Mary has been programming using Visual Basic and has told William about the Radom Number facility that the programming language provides.
William never takes anything for granted and says to Mary:
“I wonder how good this random number generator is.
Suppose you write a program to generate random positive integers in the range 1 to 1000.
Generate them in blocks of 10, 100, 1000, 10000.
For each block test how many odd and even numbers there are in the block.
Then display for each block, in some graphical manner, the number of odd integers and the number of even integers.
If the random generator is good then for the blocks with 10000 numbers the numbers of odd and the numbers of even should be close to equal.
BUT what will the results for the smaller blocks look like?”
Write the program that William has asked Mary to construct.
Allow William to input how many times he wants the program to be run ensuring that different sets of numbers are generated for each run.

Dani AI

Generated

Notes tied to 's test and practical tips for implementation and interpretation.

Use one Random instance for the whole run (do not recreate Random in a tight loop — multiple instances seeded by the clock can give repeated sequences). To make each program run produce a different sequence either rely on Random's default seed or create a seed from a cryptographic source and record it if you need reproducibility. In VB6 use Randomize once before calls to Rnd.

Example VB.NET pattern (reuse the Random, count parity, push results to a chart):

' Form-level
Private rnd As New Random()

Private Sub RunSamples(samples As Integer, minVal As Integer, maxVal As Integer)
    Dim odd As Integer = 0
    Dim even As Integer = 0
    For i As Integer = 1 To samples
        Dim v As Integer = rnd.Next(minVal, maxVal + 1) ' Next upper bound is exclusive
        If (v And 1) = 1 Then
            odd += 1
        Else
            even += 1
        End If
    Next
    Chart1.Series("Odd").Points.AddY(odd)
    Chart1.Series("Even").Points.AddY(even)
End Sub

Statistical note: parity is a 50/50 outcome, so the odd-count follows a Binomial(n, 0.5). Its mean is n/2 and its standard deviation is 0.5sqrt(n). Typical 95% ranges (mean ± 2sd) shrink relative to n as n grows — small blocks show large proportional swings, large blocks look very balanced. This explains why small-block results vary visibly while very large blocks converge.

Troubleshooting and refinements: avoid reseeding frequently; use System.Security.Cryptography.RandomNumberGenerator if higher-quality seeding is needed; remember Random.Next upper bound is exclusive; update the UI from a background thread via Invoke if generating large batches; aggregate results or use the built-in Chart control for clear bars. For reference: System.Random documentation and Binomial distribution — Wikipedia.

Recommended Answers

All 3 Replies

Nice, heart touching story.

Agreed, seems that William is not as bright as we thought.

hahahaha...too bad for William..lt me kno if u manage to do d program..lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.