Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
Set 1 (Random Numbers, Arrays and Matrices)
- Generating Random Characters
CPP
#include<bits/stdc++.h>
using namespace std;
#define RUN 5
#define MAX 25
int main()
{
srand ( time (NULL));
for ( int i=1; i<=RUN; i++)
printf ( "%c\n" , 'a' + rand () % MAX);
return (0);
}
|
Java
import java.util.Random;
class GeneratingRandomCharacters {
static final int RUN = 5 ;
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
RUN = 5
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
{
const int RUN = 5;
const int MAX = 25;
static void Main( string [] args)
{
Random rnd = new Random();
for ( int i = 1; i <= RUN; i++)
{
Console.WriteLine(( char )( 'a' + rnd.Next(MAX)));
}
}
}
}
|
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);
}
|
- Generating Random Strings
CPP
#include<bits/stdc++.h>
using namespace std;
#define RUN 100000
#define MAX 25
#define MAXLEN 100
int main()
{
srand ( time (NULL));
int LEN;
for ( int i=1; i<=RUN; i++)
{
LEN = 1 + rand () % MAXLEN;
printf ( "%d\n" , LEN);
for ( int j=1; j<=LEN; j++)
printf ( "%c" , 'a' + rand () % MAX);
printf ( "\n" );
}
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);
stringBuilder.append(length).append( '\n' );
for ( int j = 1 ; j <= length; j++) {
char randomChar = ( char ) ( 'a' + random.nextInt(MAX));
stringBuilder.append(randomChar);
}
System.out.print(stringBuilder.toString());
stringBuilder.setLength( 0 );
}
}
}
|
Python3
import random
import string
RUN = 100000
MAX = 25
MAXLEN = 100
def generate_random_string(length):
return ''.join(random.choice(string.ascii_lowercase) for _ in range (length))
def main():
for i in range ( 1 , RUN + 1 ):
length = 1 + random.randint( 0 , MAXLEN)
print (length)
random_str = generate_random_string(length)
print (random_str)
if __name__ = = "__main__" :
random.seed()
main()
|
C#
using System;
using System.Text;
class Program
{
const int RUN = 100000;
const int MAX = 25;
const int MAXLEN = 100;
static void Main()
{
{
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder();
for ( int i = 1; i <= RUN; i++)
{
int length = 1 + random.Next(MAXLEN);
stringBuilder.AppendLine(length.ToString());
for ( int j = 1; j <= length; j++)
{
char randomChar = ( char )( 'a' + random.Next(MAX));
stringBuilder.Append(randomChar);
}
Console.Write(stringBuilder.ToString());
stringBuilder.Clear();
}
}
}
}
|
Javascript
const RUN = 100000;
const MAX = 25;
const MAXLEN = 100;
let LEN;
for (let i = 1; i <= RUN; i++) {
LEN = 1 + Math.floor(Math.random() * MAXLEN);
console.log(LEN);
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
#include<bits/stdc++.h>
using namespace std;
#define RUN 1000
#define MAX 25
#define MAXNUM 20
#define MAXLEN 20
int main()
{
srand ( time (NULL));
int NUM;
int LEN;
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;
for ( int j=1; j<=LEN; j++)
printf ( "%c" , 'a' + rand () % MAX);
printf ( " " );
}
printf ( "\n" );
}
return (0);
}
|
Java
import java.util.Random;
public class Main {
private static final int RUN = 1000 ;
private static final int MAX = 25 ;
private static final int MAXNUM = 20 ;
private static final int MAXLEN = 20 ;
public static void main(String[] args)
{
Random rand = new Random();
int NUM;
int LEN;
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);
for ( int j = 1 ; j <= LEN; j++)
System.out.print(
( char )( 'a' + rand.nextInt(MAX)));
System.out.print( " " );
}
System.out.println();
}
}
}
|
Python3
import random
import string
RUN = 1000
MAX = 25
MAXNUM = 20
MAXLEN = 20
for i in range (RUN):
NUM = random.randint( 1 , MAXNUM)
print (NUM)
for k in range (NUM):
LEN = random.randint( 1 , MAXLEN)
s = ''.join(random.choices(string.ascii_lowercase, k = LEN ))
print (s, end = ' ' )
print ()
|
C#
using System;
using System.IO;
class Program {
const int RUN = 1000;
const int MAX = 25;
const int MAXNUM = 20;
const int MAXLEN = 20;
static void Main()
{
Random random = new Random();
int NUM;
int LEN;
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);
for ( int j = 1; j <= LEN; j++) {
Console.Write(
( char )( 'a' + random.Next(MAX)));
}
Console.Write( " " );
}
Console.WriteLine();
}
}
}
|
Javascript
const RUN = 1000;
const MAX = 25;
const MAXNUM = 20;
const MAXLEN = 20;
function generateTestData() {
let result = "" ;
const rand = new Math.seedrandom();
let NUM;
let LEN;
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);
let str = "" ;
for (let j = 1; j <= LEN; j++) {
str += String.fromCharCode(97 + Math.floor(rand() * MAX));
}
result += str + " " ;
}
result += "\n" ;
}
return result;
}
|
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.
Last Updated :
31 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...