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 review-team@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.
Article Tags :
Recommended Articles