Created
June 2, 2024 19:35
-
-
Save Abhinav1217/dfaff9c4b5b150abd6aaac9aa88fe874 to your computer and use it in GitHub Desktop.
A collection of utility functions for generating random data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* RandomUtil.ts | |
* | |
* A collection of utility functions for generating random data. | |
* | |
* Attribution: This file was inspired by various online resources and personal coding experience. | |
* | |
* ## Contents: | |
* - `getRandomInt(min, max)`: Generates a random integer within a specified range. | |
* - `getRandomFloat(min, max)`: Generates a random floating-point number within a specified range. | |
* - `getRandomBool()`: Generates a random boolean value. | |
* - `getRandomString(length)`: Generates a random alphanumeric string of a specified length. | |
* - `getRandomEmail()`: Generates a random email address. | |
* - `getRandomPrice(minPrice, maxPrice, increment)`: Generates a random price within a specified range and precision. | |
* - `getRandomDate(startDate, endDate)`: Generates a random date between two specified dates. | |
*/ | |
/** | |
* Generates a random integer within the specified range. | |
* | |
* @param {number} min - The minimum value of the range. | |
* @param {number} max - The maximum value of the range. | |
* @returns {number} A random integer within the range [min, max]. | |
*/ | |
export function getRandomInteger(min: number, max: number): number { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
/** | |
* Generates a random floating-point number within the specified range. | |
* | |
* @param {number} min - The minimum value of the range. | |
* @param {number} max - The maximum value of the range. | |
* @returns {number} A random floating-point number within the range [min, max]. | |
*/ | |
export function getRandomFloat(min: number, max: number): number { | |
return Math.random() * (max - min) + min; | |
} | |
/** | |
* Generates a random boolean value. | |
* | |
* @returns {boolean} A random boolean value (true or false). | |
*/ | |
export function getRandomBool(): boolean { | |
return Math.random() < 0.5; | |
} | |
/** | |
* Generates a random alphanumeric string of a specified length. | |
* | |
* @param {number} length - The desired length of the string. | |
* @returns {string} A random alphanumeric string of the specified length. | |
*/ | |
export function getRandomString(length: number): string { | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
let result = ''; | |
for (let i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * characters.length)); | |
} | |
return result; | |
} | |
/** | |
* Generates a random email address. | |
* | |
* @param {number} length - The length of the username part of the email. | |
* @returns {string} A randomly generated email address. | |
*/ | |
export function getRandomEmail(length: number = 8): string { | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
let username = ''; | |
for (let i = 0; i < length; i++) { | |
username += characters.charAt(Math.floor(Math.random() * characters.length)); | |
} | |
return `${username}@example.com`; | |
} | |
/** | |
* Generates a random price within a specified range and precision. | |
* | |
* @param {number} minPrice - The minimum price. | |
* @param {number} maxPrice - The maximum price. | |
* @param {number} increment - The price increment. | |
* @param {boolean} includeFractionCost - Include fractional part of price. | |
* @returns {number} A random price within the range [minPrice, maxPrice] with a precision of 0.00 or 0.25. | |
*/ | |
export function getRandomPrice( | |
minPrice: number = 250, | |
maxPrice: number = 900, | |
increment: number = 25, | |
includeFractionCost: boolean = true | |
): number { | |
const range = (maxPrice - minPrice) / increment; | |
const randomIncrement = Math.floor(Math.random() * (range + 1)); | |
const randomPrice = minPrice + randomIncrement * increment; | |
// const decimalPart = Math.random() < 0.5 ? 0.0 : 0.25; | |
// Determine the decimal part based on the includeFractionCost flag | |
let decimalPart = 0; | |
if (includeFractionCost) { | |
// Select a random fraction (00, 25, 50, 75) | |
const fractions = [0, 25, 50, 75]; | |
decimalPart = fractions[Math.floor(Math.random() * fractions.length)]; | |
} | |
return randomPrice + decimalPart; | |
} | |
/** | |
* Generates a random date between two specified dates. | |
* | |
* @param {Date} startDate - The start date. | |
* @param {Date} endDate - The end date. | |
* @returns {Date} A random date between startDate and endDate. | |
*/ | |
export function getRandomDate(startDate: Date, endDate: Date): Date { | |
const startTimestamp = startDate.getTime(); | |
const endTimestamp = endDate.getTime(); | |
const randomTimestamp = Math.random() * (endTimestamp - startTimestamp) + startTimestamp; | |
const randomDate = new Date(randomTimestamp); | |
randomDate.setUTCHours(0, 0, 0, 0); | |
return randomDate; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment