Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,406 questions

56,397 answers

573 users

How to create a list of random file names, including extension, dates, and file size in TypeScript

1 Answer

0 votes
// Generate a random integer in [min, max)
function randomInt(min: number, max: number): number {
    return Math.floor(Math.random() * (max - min)) + min;
}

// Generate a random string of given length
function generateRandomString(length: number): string {
    const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    let result = "";

    for (let i: number = 0; i < length; i++) {
        result += charset[randomInt(0, charset.length)];
    }

    return result;
}

// Generate a random date
function generateRandomDate(): string {
    const year: number = randomInt(2000, 2021);  // 2000–2020
    const month: number = randomInt(1, 13);      // 1–12
    const day: number = randomInt(1, 29);        // 1–28

    return `${year.toString().padStart(4, "0")}-${month
        .toString()
        .padStart(2, "0")}-${day.toString().padStart(2, "0")}`;
}

// Generate a random file size
function generateRandomFileSize(): number {
    return randomInt(1, 100001); // 1–100000 bytes
}

const extensions: string[] = [".txt", ".jpg", ".png", ".cpp", ".pdf"];
const numberOfFiles = 10;
const fileLength = 9;

for (let i: number = 0; i < numberOfFiles; i++) {
    const fileName = generateRandomString(fileLength);
    const extension = extensions[randomInt(0, extensions.length)];
    const date = generateRandomDate();
    const fileSize = generateRandomFileSize();

    console.log(`${fileName}${extension} ${date} ${fileSize} bytes`);
}



/*
run:

u0kxUsC1S.png 2009-12-01 14836 bytes
3GPrsXtI0.jpg 2007-07-21 11436 bytes
wlIKGsXPv.jpg 2001-01-17 43676 bytes
NVFFHXgLQ.pdf 2005-02-10 62508 bytes
rsBeakICg.jpg 2009-10-14 90438 bytes
fMfz8sNQu.pdf 2016-11-09 53066 bytes
3ZPuAjv9v.txt 2010-12-03 11045 bytes
aTE2qYJEv.pdf 2005-07-24 34763 bytes
iwP31dCms.pdf 2016-06-08 46436 bytes
XIzzxpOGX.cpp 2014-09-04 6458 bytes

*/

 



answered May 16 by avibootz

Related questions

...