All examples
Scientific computing

Balanced and weighted dice with a parallel loop

Run only the 1000 paired roll loop on four local workers, then calculate serial face counts and means for the balanced and weighted dice.

repeat_dice.aner
// Uses Philox addressed sampling with explicit seed and draw identities.
// Weighted dice: serial setup, 1000 parallel paired rolls, serial analysis.
// Requires the updated native Aner engine and an effective CPU budget admitting four workers.
// The OS schedules worker threads; Aner does not pin each worker to a logical CPU.
import aner.parallel
import aner.random

// The supplied fixtures have finite, positive Float64 weights.
// Select faces 1..weights.len() by their cumulative relative weights.
fn dice(weights: List, seed: Int64, draw: Int64) -> Int64 {
    var total = 0.0
    for index in Range(weights.len()) {
        total += cast<Float64>(weights.get(index))
    }
    let threshold = random.uniform(seed: seed, draw: draw) * total
    var cumulative = 0.0
    for index in Range(weights.len()) {
        cumulative += cast<Float64>(weights.get(index))
        if threshold < cumulative { return index + 1 }
    }
    return weights.len()
}

// Each invocation owns its parameter copies; two draws produce one paired row.
fn roll_pair(parameters: List, iteration: Int64, seed: Int64) -> List {
    let balanced = cast<List>(parameters.get(0))
    let unbalanced = cast<List>(parameters.get(1))
    return List(dice(balanced, seed, draw: 0), dice(unbalanced, seed, draw: 1))
}

// Serial histogram over the retained Float64 face values. No numeric narrowing.
fn face_counts(rolls: List) -> Array<Int64> {
    let counts = Array<Int64>(6, 0)
    for iteration in Range(rolls.len()) {
        let roll = cast<Float64>(rolls.get(iteration))
        for face in Range(6) {
            if roll == Float64(face + 1) { counts[face] += 1 }
        }
    }
    return counts
}

// 1. Serial setup: the same initial weights are supplied to every repetition.
let dice_balanced = List(1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
let dice_unbalanced = List(50.0, 10.0, 10.0, 10.0, 10.0, 10.0)
let initial_parameters = List(dice_balanced, dice_unbalanced)

// BEGIN PARALLEL
// 2. Four native worker threads share 1000 independent paired-roll tasks.
let results = parallel.repeat(iterations: 1000, task: roll_pair,
    parameters: initial_parameters, execution: parallel.local(workers: 4), seed: 42)
// END PARALLEL

// 3. Serial analysis after every paired roll is collected in iteration order.
let dice_balanced_rolls = results.column(column: 0)
let dice_unbalanced_rolls = results.column(column: 1)
print("Balanced average: " + String(results.mean(column: 0)))
print("Unbalanced average: " + String(results.mean(column: 1)))
let balanced_counts = face_counts(dice_balanced_rolls)
let unbalanced_counts = face_counts(dice_unbalanced_rolls)
print("Face | Balanced | Unbalanced")
for face in Range(6) {
    print(String(face + 1) + " | " + String(balanced_counts[face]) +
        " | " + String(unbalanced_counts[face]))
}
// No files are created. To save all paired outcomes, choose a new CSV path:
// results.save_csv("dice-results.csv")

Make it your experiment.

Use Aner 0.0.3 or newer. Save this program as examples/parallel/repeat_dice.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/parallel/repeat_dice.aner
aner run examples/parallel/repeat_dice.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Use the updated native Aner executable with checked parallel.repeat tasks and SimulationResults. Use matching editor integration with argument and result help for repetition tasks. The explicit four worker loop requires at least four admitted CPU slots as its worker thread admission budget. The OS schedules these threads; the request does not pin them to four CPUs. Setup and face count analysis execute serially; only the 1000 paired rolls run on local workers. Checking performs no repetitions. Running needs no external data, network, service or credential and creates no file by default; the optional CSV export is commented. The revised source requires Aner 0.0.4 or newer; earlier executables may reject the syntax.

Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner