Use crypto.randomBytes instead of Math.random
This commit is contained in:
parent
60d41c2ef8
commit
7473a2f724
1 changed files with 12 additions and 3 deletions
|
@ -4,6 +4,7 @@ import { createSVGWindow } from 'svgdom'
|
|||
import { SVG, registerWindow } from '@svgdotjs/svg.js'
|
||||
import * as fs from "node:fs/promises";
|
||||
import sharp from "sharp";
|
||||
import crypto from "crypto";
|
||||
|
||||
const BASE_SVG = await fs.readFile("assets/ElectoralCollege2024.svg", "utf-8");
|
||||
|
||||
|
@ -17,8 +18,8 @@ export function MakePrediction(bias: number, gopcandidate : string, demcandidate
|
|||
var demVotes = 0;
|
||||
var draw = SVG(window.document.documentElement);
|
||||
draw.svg(BASE_SVG);
|
||||
Object.keys(ELECTORAL_COLLEGE).forEach(state => {
|
||||
if ((GOP_WIN_ODDS as any)[state] >= bias + Math.random()) {
|
||||
for (const state of Object.keys(ELECTORAL_COLLEGE)) {
|
||||
if ((GOP_WIN_ODDS as any)[state] >= bias + await betterRandom()) {
|
||||
election[state] = "R";
|
||||
gopVotes += (ELECTORAL_COLLEGE as any)[state];
|
||||
// @ts-ignore
|
||||
|
@ -30,7 +31,7 @@ export function MakePrediction(bias: number, gopcandidate : string, demcandidate
|
|||
// @ts-ignore
|
||||
draw.find(`#${state}`).fill("#1C408C");
|
||||
}
|
||||
});
|
||||
};
|
||||
var s = sharp(Buffer.from(draw.svg()));
|
||||
var png = await s.png().toBuffer();
|
||||
res({
|
||||
|
@ -53,4 +54,12 @@ export interface Prediction {
|
|||
winner: string,
|
||||
svg: string,
|
||||
png: Buffer,
|
||||
}
|
||||
|
||||
export function betterRandom() : Promise<number> {
|
||||
return new Promise(res => {
|
||||
crypto.randomBytes(4, (err, buf) => {
|
||||
res(buf.readUInt32BE(0) / 0x100000000);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue