/*
This program generates random lottery numbers for:
- 6 distinct numbers out of 37
- 1 distinct number out of 7 (power number)
It uses:
- TypeScript's strong typing
- Fisher–Yates shuffle for uniform randomness
- Math.random() for random number generation
- clean functions and idiomatic TypeScript style
*/
/*
shuffle<T>(array):
Implements the Fisher–Yates shuffle.
This algorithm:
- runs in O(n)
- produces a uniform random permutation
- is the standard way to shuffle arrays correctly
*/
function shuffle<T>(array: T[]): T[] {
for (let i: number = array.length - 1; i > 0; i--) {
const j: number = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
/*
pickDistinctNumbers(count, max):
Returns `count` distinct random numbers from the range 1..max.
Algorithm:
- Build an array [1, 2, ..., max]
- Shuffle it using Fisher–Yates
- Take the first `count` numbers
*/
function pickDistinctNumbers(count: number, max: number): number[] {
const numbers: number[] = Array.from({ length: max }, (_, i) => i + 1);
shuffle(numbers);
return numbers.slice(0, count);
}
/*
pickPowerNumber(max):
Returns a single random number in the range 1..max.
*/
function pickPowerNumber(max: number): number {
return Math.floor(Math.random() * max) + 1;
}
/*
Main program
*/
const MAIN_COUNT: number = 6;
const MAIN_MAX: number = 37;
const POWER_MAX: number = 7;
// Generate main numbers (distinct)
const mainNumbers: number[] = pickDistinctNumbers(MAIN_COUNT, MAIN_MAX)
.sort((a: number, b: number) => a - b);
// Generate power number
const powerNumber: number = pickPowerNumber(POWER_MAX);
// Output results
console.log("Main numbers (6 out of 37):", mainNumbers.join(" "));
console.log("Power number (1 out of 7):", powerNumber);
/*
run:
Main numbers (6 out of 37): 4 7 11 16 30 33
Power number (1 out of 7): 2
*/