Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings) Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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); } Java import java.util.Random; public class RandomStringGenerator { private static final int RUN = 100000; private static final int MAX = 25; private static final int MAXLEN = 100; public static void main(String[] args) { Random random = new Random(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 1; i <= RUN; i++) { int length = 1 + random.nextInt(MAXLEN); // Append the length of the string to StringBuilder stringBuilder.append(length).append('\n'); // Append the characters of the string to StringBuilder for (int j = 1; j <= length; j++) { char randomChar = (char) ('a' + random.nextInt(MAX)); stringBuilder.append(randomChar); } // Print the complete string at once System.out.print(stringBuilder.toString()); stringBuilder.setLength(0); // Clear StringBuilder } } } Python3 # Python program to generate test cases for random strings import random import string # Define the number of runs for the test data generated RUN = 100000 # Define the range of the test data generated # Here it is 'a' to 'z' MAX = 25 # Define the maximum length of the string MAXLEN = 100 def generate_random_string(length): """Generate a random string of given length.""" return ''.join(random.choice(string.ascii_lowercase) for _ in range(length)) def main(): # Uncomment the below line to store # the test data in a file # with open("Test_Cases_Random_String.in", "w") as f: for i in range(1, RUN + 1): # Generate a random length for the string length = 1 + random.randint(0, MAXLEN) # First print the length of the string print(length) # Then print the characters of the string random_str = generate_random_string(length) print(random_str) # Uncomment the below line to store # the test data in a file # f.close() if __name__ == "__main__": # For random values every time random.seed() # Call the main function main() C# using System; using System.Text; class Program { const int RUN = 100000; const int MAX = 25; const int MAXLEN = 100; static void Main() { // Uncomment the below line to store the test data in a file // using (System.IO.StreamWriter file = new System.IO.StreamWriter("Test_Cases_Random_String.txt")) { Random random = new Random(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 1; i <= RUN; i++) { int length = 1 + random.Next(MAXLEN); // First append the length of the string to StringBuilder stringBuilder.AppendLine(length.ToString()); // Then append the characters of the string to StringBuilder for (int j = 1; j <= length; j++) { char randomChar = (char)('a' + random.Next(MAX)); stringBuilder.Append(randomChar); } // Print the complete string at once Console.Write(stringBuilder.ToString()); stringBuilder.Clear(); } // Uncomment the below line to store the test data in a file // file.Close(); } } } 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()); If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. 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. Comment More infoAdvertise with us Next Article Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings) R Rachit Belweriar Improve Article Tags : Competitive Programming DSA Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like