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 25int 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 generatedstatic 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 generatedRUN = 5# Define the range of the test data generated# Here it is 'a' to 'z'MAX = 25for 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 100int 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 generatedconst RUN = 100000;// Define the range of the test data generated// Here it is 'a' to 'z'const MAX = 25;// Define the maximum length of stringconst MAXLEN = 100;// driver program// For random values every timelet LEN; // Length of stringfor (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 20int 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 stringsimport randomimport string# Define the number of runs for the test data generatedRUN = 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 arrayMAXNUM = 20# Define the maximum length of stringMAXLEN = 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 stringsusing 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 generatedconst 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 arrayconst MAXNUM = 20;// Define the maximum length of stringconst MAXLEN = 20;function generateTestData() {let result = "";// For random values every timeconst rand = new Math.seedrandom();let NUM; // Number of strings in arraylet LEN; // Length of stringfor (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 zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek 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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



