Fix random number generation in Dice game

This commit is contained in:
Lncvrt 2025-06-24 20:46:33 -07:00
parent 96cb799cc7
commit 3030919616
No known key found for this signature in database
GPG key ID: 032FCF8600975467

View file

@ -3,7 +3,7 @@ import { error, json } from '@sveltejs/kit';
import { db } from '$lib/server/db'; import { db } from '$lib/server/db';
import { user } from '$lib/server/db/schema'; import { user } from '$lib/server/db/schema';
import { eq } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
import { randomBytes } from 'crypto'; import { randomInt } from 'crypto';
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
interface DiceRequest { interface DiceRequest {
@ -54,7 +54,7 @@ export const POST: RequestHandler = async ({ request }) => {
throw new Error(`Insufficient funds. You need *${roundedAmount.toFixed(2)} but only have *${roundedBalance.toFixed(2)}`); throw new Error(`Insufficient funds. You need *${roundedAmount.toFixed(2)} but only have *${roundedBalance.toFixed(2)}`);
} }
const gameResult = Math.floor(randomBytes(1)[0] / 42.67) + 1; // This gives us a number between 1-6 const gameResult = randomInt(1, 7);
const won = gameResult === selectedNumber; const won = gameResult === selectedNumber;
const multiplier = 3; const multiplier = 3;