4.1.1.8 - Random numbers and controlled simulations (Java)

4.1.1.8 - Random numbers and controlled simulations (Java)

Generate values within precise Java ranges and design simulations whose random behaviour can be checked without guessing one output.

Java route status: incomplete: runnable support pending. Code tasks here are written practice.

Random ranges and distributions

A random-number generator supplies values that can drive a game, a sample or a simulation.

Pseudorandom sequence

A sequence generated by a deterministic algorithm from an initial state or seed, designed to behave like random values for its intended use.

Java
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random generator = new Random();
        int die = generator.nextInt(6) + 1;
        int temperature = generator.nextInt(11) + 15;
        double fraction = generator.nextDouble();
        System.out.println(die);
        System.out.println(temperature);
        System.out.println(fraction);
    }
}

nextInt(bound) produces an integer from zero inclusive to bound exclusive. The bound must be positive. Thus a bound of 6 permits 0 to 5; adding 1 gives the six die faces. A bound of 11 then adding 15 gives integers 15 to 25 inclusive. A general inclusive integer range from low to high uses nextInt(high - low + 1) + low, provided the width is positive and within the integer range.

nextDouble() produces a value at least 0.0 and less than 1.0. Scaling by 10 and adding 20 gives real values from 20.0 inclusive to 30.0 exclusive. Uniform integer generation gives each permitted value approximately equal probability over many trials; it does not guarantee equal counts in a short sample. Rounding a real random value to make integers can give unequal endpoint probabilities, so use the bounded integer operation for uniform integers. Random is not a security generator for secret keys.

Testing nondeterministic programs

Random behaviour requires checking both deterministic logic and statistical expectations.

Using a known seed, such as new Random(42L), lets repeated executions with the same generator calls reproduce the same sequence. This helps reproduce a defect. The call order matters: an added draw consumes another sequence value. A seed does not mean every draw is identical, and one repeatable sequence does not establish that all possible inputs are handled.

To test the decision logic, supply chosen draws to a separate calculation or trace it with explicit values. If a success occurs for a die value of at least 5, chosen values 4, 5 and 6 check the threshold and both success values. Values 1 and 6 check range endpoints. A long random sample can check that outputs stay in range and that frequencies are plausible, but cannot prove fairness or replace boundary tests. Requiring exactly ten occurrences of each face in sixty rolls would reject many reasonable random sequences.

Use repeatable draws to reproduce a failure, chosen boundary draws to check logic, and many trials to investigate frequencies. These checks answer different questions.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account