Skip to content
Related Articles

Related Articles

Improve Article
Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
  • Difficulty Level : Expert
  • Last Updated : 30 May, 2018

Set 1 (Random Numbers, Arrays and Matrices)

  • Generating Random Characters




    // 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);
    }
  • Generating Random Strings




    // 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);
    }
  • Generating Array of Random Strings




    // 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);
    }

This article is contributed by Rachit Belweriar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :