Sunday, November 17, 2024
Google search engine
HomeData Modelling & AITest Case Generation | Set 2 ( Random Characters, Strings and Arrays...

Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)

Set 1 (Random Numbers, Arrays and Matrices)
 

  • Generating Random Characters
     

CPP




// A C++ Program to generate test cases for
// random characters
#include<bits/stdc++.h>
using namespace std;
 
// Define the number of runs for the test data
// generated
#define RUN 5
 
// Define the range of the test data generated
// Here it is 'a' to 'z'
#define MAX 25
 
int main()
{
    // Uncomment the below line to store
    // the test data in a file
    // freopen ("Test_Cases_Random_Character.in", "w", stdout);
 
    // For random values every time
    srand(time(NULL));
 
    for (int i=1; i<=RUN; i++)
        printf("%c\n", 'a' + rand() % MAX);
 
    // Uncomment the below line to store
    // the test data in a file
    // fclose(stdout);
    return(0);
}


Java




import java.util.Random;
 
class GeneratingRandomCharacters {
// Define the number of runs for the test data generated
static final int RUN = 5;
 
// Define the range of the test data generated
// Here it is 'a' to 'z'
static final int MAX = 25;
 
public static void main(String[] args) {
    Random rand = new Random();
    for (int i = 0; i < RUN; i++) {
        char randomChar = (char) ('a' + rand.nextInt(MAX));
        System.out.println(randomChar);
    }
}
}


Python3




import random
 
# Define the number of runs for the test data generated
RUN = 5
 
# Define the range of the test data generated
# Here it is 'a' to 'z'
MAX = 25
 
for i in range(RUN):
    random_char = chr(ord('a') + random.randint(0, MAX))
    print(random_char)


C#




using System;
 
namespace RandomCharacterTestCases
{
    class Program
    {
        // Define the number of runs for the test data
        // generated
        const int RUN = 5;
 
        // Define the range of the test data generated
        // Here it is 'a' to 'z'
        const int MAX = 25;
 
        static void Main(string[] args)
        {
            // Uncomment the below line to store
            // the test data in a file
            // Console.SetOut(new System.IO.StreamWriter("Test_Cases_Random_Character.in"));
 
            // For random values every time
            Random rnd = new Random();
 
            for (int i = 1; i <= RUN; i++)
            {
                Console.WriteLine((char)('a' + rnd.Next(MAX)));
            }
 
            // Uncomment the below line to store
            // the test data in a file
            // Console.Out.Close();
        }
    }
}
// This code is contributed by divyansh2212


Javascript




let requiredNumbers = 5;
let lowerBound = 0;
let upperBound = 25;
 
for(let i = 0; i < requiredNumbers; i++)
{
    let a = String.fromCharCode(97 + Math.floor(Math.random() *
    (upperBound - lowerBound)) + lowerBound);
    console.log(a);
}
 
// This code is contributed by Shubham Singh


  • Generating Random Strings
     

CPP




// A C++ Program to generate test cases for
// random strings
#include<bits/stdc++.h>
using namespace std;
 
// Define the number of runs for the test data
// generated
#define RUN 100000
 
// Define the range of the test data generated
// Here it is 'a' to 'z'
#define MAX 25
 
// Define the maximum length of string
#define MAXLEN 100
 
int main()
{
    // Uncomment the below line to store
    // the test data in a file
    // freopen ("Test_Cases_Random_String.in", "w", stdout);
 
    //For random values every time
    srand(time(NULL));
 
    int LEN; // Length of string
 
    for (int i=1; i<=RUN; i++)
    {
        LEN = 1 + rand() % MAXLEN;
 
        // First print the length of string
        printf("%d\n", LEN);
 
        // Then print the characters of the string
        for (int j=1; j<=LEN; j++)
            printf("%c", 'a' + rand() % MAX);
 
        printf("\n");
    }
 
    // Uncomment the below line to store
    // the test data in a file
    // fclose(stdout);
    return(0);
}


Javascript




// Define the number of runs for the test data generated
const RUN = 100000;
 
// Define the range of the test data generated
// Here it is 'a' to 'z'
const MAX = 25;
 
// Define the maximum length of string
const MAXLEN = 100;
 
// driver program
// For random values every time
let LEN; // Length of string
 
for (let i = 1; i <= RUN; i++) {
  LEN = 1 + Math.floor(Math.random() * MAXLEN);
 
  // First print the length of string
  console.log(LEN);
 
  // Then print the characters of the string
  let str = "";
  for (let j = 1; j <= LEN; j++)
      str += String.fromCharCode(97 + Math.floor(Math.random() * MAX));
  console.log(str);
}


  • Generating Array of Random Strings 
     

CPP




// A C++ Program to generate test cases for
// random strings
#include<bits/stdc++.h>
using namespace std;
 
// Define the number of runs for the test data
// generated
#define RUN 1000
 
// Define the range of the test data generated
// Here it is 'a' to 'z'
#define MAX 25
 
// Define the range of number of strings in the array
#define MAXNUM 20
 
// Define the maximum length of string
#define MAXLEN 20
 
int main()
{
    // Uncomment the below line to store
    // the test data in a file
    // freopen ("Test_Cases_Array_of_Strings.in", "w", stdout);
 
    //For random values every time
    srand(time(NULL));
 
    int NUM; // Number of strings in array
 
    int LEN; // Length of string
 
    for (int i=1; i<=RUN; i++)
    {
        NUM = 1 + rand() % MAXNUM;
        printf("%d\n", NUM);
 
        for (int k=1; k<=NUM; k++)
        {
            LEN = 1 + rand() % MAXLEN;
 
            // Then print the characters of the string
            for (int j=1; j<=LEN; j++)
                printf("%c", 'a' + rand() % MAX);
 
            printf(" ");
        }
        printf("\n");
    }
    // Uncomment the below line to store
    // the test data in a file
    // fclose(stdout);
    return(0);
}


Java




import java.util.Random;
 
public class Main {
 
    // Define the number of runs for the test data generated
    private static final int RUN = 1000;
 
    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    private static final int MAX = 25;
 
    // Define the range of number of strings in the array
    private static final int MAXNUM = 20;
 
    // Define the maximum length of string
    private static final int MAXLEN = 20;
 
    public static void main(String[] args)
    {
 
        // Uncomment the below line to store
        // the test data in a file
        // System.setOut(new PrintStream(new
        // File("Test_Cases_Array_of_Strings.in")));
 
        // For random values every time
        Random rand = new Random();
 
        int NUM; // Number of strings in array
 
        int LEN; // Length of string
 
        for (int i = 1; i <= RUN; i++) {
            NUM = 1 + rand.nextInt(MAXNUM);
            System.out.println(NUM);
 
            for (int k = 1; k <= NUM; k++) {
                LEN = 1 + rand.nextInt(MAXLEN);
 
                // Then print the characters of the string
                for (int j = 1; j <= LEN; j++)
                    System.out.print(
                        (char)('a' + rand.nextInt(MAX)));
 
                System.out.print(" ");
            }
            System.out.println();
        }
 
        // Uncomment the below line to store
        // the test data in a file
        // System.out.close();
    }
}


Python3




# A Python3 program to generate test cases for random strings
import random
import string
 
# Define the number of runs for the test data generated
RUN = 1000
 
# Define the range of the test data generated
# Here it is 'a' to 'z'
MAX = 25
 
# Define the range of number of strings in the array
MAXNUM = 20
 
# Define the maximum length of string
MAXLEN = 20
 
# Uncomment the below line to store
# the test data in a file
# sys.stdout = open("Test_Cases_Array_of_Strings.in", "w")
 
for i in range(RUN):
    NUM = random.randint(1, MAXNUM)
    print(NUM)
 
    for k in range(NUM):
        LEN = random.randint(1, MAXLEN)
 
        # Then print the characters of the string
        s = ''.join(random.choices(string.ascii_lowercase, k=LEN))
        print(s, end=' ')
 
    print()
# Uncomment the below line to store
# the test data in a file
# sys.stdout.close()


C#




// A C# Program to generate test cases for
// random strings
using System;
using System.IO;
 
class Program {
    // Define the number of runs for the test data generated
    const int RUN = 1000;
 
    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    const int MAX = 25;
 
    // Define the range of number of strings in the array
    const int MAXNUM = 20;
 
    // Define the maximum length of string
    const int MAXLEN = 20;
 
    static void Main()
    {
        // Uncomment the below line to store
        // the test data in a file
        // Console.SetOut(new
        // StreamWriter("Test_Cases_Array_of_Strings.in"));
 
        // For random values every time
        Random random = new Random();
 
        int NUM; // Number of strings in array
        int LEN; // Length of string
 
        for (int i = 1; i <= RUN; i++) {
            NUM = 1 + random.Next(MAXNUM);
            Console.WriteLine(NUM);
 
            for (int k = 1; k <= NUM; k++) {
                LEN = 1 + random.Next(MAXLEN);
 
                // Then print the characters of the string
                for (int j = 1; j <= LEN; j++) {
                    Console.Write(
                        (char)('a' + random.Next(MAX)));
                }
 
                Console.Write(" ");
            }
 
            Console.WriteLine();
        }
 
        // Uncomment the below line to store
        // the test data in a file
        // Console.Out.Close();
    }
}


Javascript




// Define the number of runs for the test data generated
const RUN = 1000;
 
// Define the range of the test data generated
// Here it is 'a' to 'z'
const MAX = 25;
 
// Define the range of number of strings in the array
const MAXNUM = 20;
 
// Define the maximum length of string
const MAXLEN = 20;
 
function generateTestData() {
let result = "";
 
// For random values every time
const rand = new Math.seedrandom();
 
let NUM; // Number of strings in array
 
let LEN; // Length of string
 
for (let i = 1; i <= RUN; i++) {
NUM = 1 + Math.floor(rand() * MAXNUM);
result += NUM + "\n";
for (let k = 1; k <= NUM; k++) {
  LEN = 1 + Math.floor(rand() * MAXLEN);
 
  // Then print the characters of the string
  let str = "";
  for (let j = 1; j <= LEN; j++) {
    str += String.fromCharCode(97 + Math.floor(rand() * MAX));
  }
 
  result += str + " ";
}
result += "\n";
}
 
return result;
}
 
// Uncomment the below line to store
// the test data in a file
// fs.writeFileSync('Test_Cases_Array_of_Strings.in', generateTestData());


This article is contributed by Rachit Belweriar. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments