import { assert } from "../../assert.js";

/**
 * Returns number from Halton Sequence identified by base and sequence index
 * Halton sequence produces numbers that appear to have random distribution
 * @see https://en.wikipedia.org/wiki/Halton_sequence
 * @param {number} base
 * @param {number} index sequence index
 * @returns {number}
 */
export function halton_sequence(base, index) {
    assert.isNonNegativeInteger(base, 'base');
    assert.isNonNegativeInteger(index, 'index');

    let result = 0;
    let f = 1;
    let i = index;

    while (i > 0) {
        f = f / base;
        result = result + f * (i % base);
        i = (i / base) >>> 0;
    }

    return result;
}
