Random Utilities
Overview
Random utilities provide a set of functions for generating random numbers and values in your game. These utilities are essential for creating variety in gameplay, procedural generation, and randomized behaviors.
Random Number Generation
Float Values
Generate random floating-point numbers within a range:
import { floatBetween } from 'dill-pixel';
// Random float between 0 and 1const opacity = floatBetween(0, 1);
// Random float for positionconst x = floatBetween(-100, 100);
Integer Values
Generate random integer numbers within a range:
import { intBetween } from 'dill-pixel';
// Random level between 1 and 10 (inclusive)const level = intBetween(1, 11);
// Random damage valueconst damage = intBetween(10, 21); // 10-20 damage
Boolean Values
Generate random boolean values:
import { bool } from 'dill-pixel';
// 50/50 chanceconst shouldSpawn = bool();const isCriticalHit = bool();
Point-Based Random Values
Generate random values between point coordinates:
import { Point } from 'pixi.js';import { floatBetweenPoint, intBetweenPoint } from 'dill-pixel';
// Define range using a pointconst range = new Point(0, 100);
// Random float between point coordinatesconst float = floatBetweenPoint(range); // 0-100 float
// Random integer between point coordinatesconst int = intBetweenPoint(range); // 0-99 integer
Common Use Cases
Procedural Generation
import { intBetween, floatBetween } from 'dill-pixel';
class LevelGenerator { generateRoom() { return { width: intBetween(10, 21), // 10-20 units wide height: intBetween(10, 21), // 10-20 units high enemies: intBetween(3, 7), // 3-6 enemies difficulty: floatBetween(1, 2), // 1.0-2.0 difficulty multiplier }; }}
Particle Effects
import { floatBetween, bool } from 'dill-pixel';
class ParticleEmitter { createParticle() { return { velocity: { x: floatBetween(-5, 5), y: floatBetween(-5, 0), }, lifetime: floatBetween(0.5, 2), rotates: bool(), // 50% chance to rotate }; }}
Enemy Behavior
import { bool, intBetween } from 'dill-pixel';
class Enemy { decideAction() { if (bool()) { // 50% chance to attack return { type: 'attack', damage: intBetween(5, 11), // 5-10 damage }; } else { // 50% chance to move return { type: 'move', distance: intBetween(1, 4), // 1-3 units }; } }}
Loot Tables
import { floatBetween, intBetween } from 'dill-pixel';
class LootSystem { generateLoot(quality: number) { return { gold: intBetween(10, 51) * quality, // 10-50 gold, scaled by quality rarity: floatBetween(0, 1) < 0.2 ? 'rare' : 'common', // 20% chance for rare quantity: intBetween(1, 4), // 1-3 items }; }}
Best Practices
-
Use appropriate functions for your needs:
// Good: Using intBetween for discrete valuesconst damage = intBetween(1, 11); // 1-10 damage// Good: Using floatBetween for continuous valuesconst speed = floatBetween(0, 1); // 0.0-1.0 speed// Good: Using bool for binary choicesconst success = bool(); // true/false -
Consider range boundaries:
// Remember: intBetween max is exclusiveconst diceRoll = intBetween(1, 7); // 1-6 (for a d6)// Remember: floatBetween max is exclusiveconst percent = floatBetween(0, 1); // 0.0-0.999... -
Use Point-based ranges when appropriate:
import { Point } from 'pixi.js';// Define reusable rangesconst damageRange = new Point(10, 21);const speedRange = new Point(0, 1);// Use consistentlyconst damage = intBetweenPoint(damageRange);const speed = floatBetweenPoint(speedRange);
Performance Considerations
Efficient Usage
// Good: Cache random values that won't changeclass Enemy { private readonly damage = intBetween(5, 11); private readonly speed = floatBetween(0.8, 1.2);
// Bad: Generating random values every frame update() { this.x += floatBetween(-1, 1); // Don't do this }
// Good: Generate random values when needed attack() { return this.damage + intBetween(-2, 3); // Base damage ± 2 }}
Tips and Tricks
-
Create helper functions for common ranges:
const randomPercent = () => floatBetween(0, 1);const randomSign = () => (bool() ? 1 : -1);const d20 = () => intBetween(1, 21); -
Combine with other utilities:
import { Point } from 'pixi.js';import { floatBetween } from 'dill-pixel';function randomPosition(bounds: { width: number; height: number }): Point {return new Point(floatBetween(0, bounds.width), floatBetween(0, bounds.height));} -
Use for weighted choices:
function getRandomRarity() {const roll = floatBetween(0, 1);if (roll < 0.1) return 'legendary'; // 10%if (roll < 0.3) return 'rare'; // 20%if (roll < 0.6) return 'uncommon'; // 30%return 'common'; // 40%}