16 lines
424 B
TypeScript
16 lines
424 B
TypeScript
import type { PieceWithIssuer } from '@/db/types';
|
|
|
|
export function rand(min: number, max: number): number {
|
|
return min + Math.random() * (max - min);
|
|
}
|
|
|
|
export function pickCoin(pool: PieceWithIssuer[]): PieceWithIssuer {
|
|
const total = pool.reduce((sum, c) => sum + c.count, 0);
|
|
let r = Math.random() * total;
|
|
for (const c of pool) {
|
|
r -= c.count;
|
|
if (r < 0) return c;
|
|
}
|
|
return pool[pool.length - 1];
|
|
}
|