The test cases are an extremely important part of any “Software/Project Testing Process”. Hence this Set will be very important for all aspiring software developers. The following are the programs to generate test cases.
- Generating Random Numbers
C++
#include<bits/stdc++.h>
using namespace std;
#define RUN 5
#define MAX 10000000
int main()
{
srand ( time (NULL));
for ( int i=1; i<=RUN; i++)
printf ( "%d\n" , rand () % MAX);
return (0);
}
|
Java
import java.io.*;
import java.util.Random;
class GeneratingRandomNumbers
{
static int requiredNumbers = 5 ;
static int lowerBound = 0 ;
static int upperBound = 1000 ;
public static void main (String[] args) throws IOException
{
Random random = new Random();
for ( int i = 0 ; i < requiredNumbers; i++)
{
int a = random.nextInt(upperBound - lowerBound) +
lowerBound;
System.out.println(a);
}
}
}
|
Python3
import random
requiredNumbers = 5 ;
lowerBound = 0 ;
upperBound = 1000 ;
if __name__ = = '__main__' :
for i in range (requiredNumbers):
a = random.randrange( 0 , upperBound -
lowerBound) + lowerBound;
print (a);
|
C#
using System;
class GeneratingRandomNumbers
{
static int requiredNumbers = 5;
static int lowerBound = 0;
static int upperBound = 1000;
public static void Main(String[] args)
{
Random random = new Random();
for ( int i = 0; i < requiredNumbers; i++)
{
int a = random.Next(upperBound - lowerBound) +
lowerBound;
Console.WriteLine(a);
}
}
}
|
Javascript
<script>
let requiredNumbers = 5;
let lowerBound = 0;
let upperBound = 1000;
for (let i = 0; i < requiredNumbers; i++)
{
let a = Math.floor(Math.random() *
(upperBound - lowerBound)) + lowerBound;
document.write(a+ "<br>" );
}
</script>
|
C++
#include<bits/stdc++.h>
using namespace std;
#define RUN 5
#define MAX 10000000
#define MAXNUM 100
int main()
{
srand ( time (NULL));
for ( int i=1; i<=RUN; i++)
{
int NUM = 1 + rand () % MAXNUM;
printf ( "%d\n" , NUM);
for ( int j=1; j<=NUM; j++)
printf ( "%d " , rand () % MAX);
printf ( "\n" );
}
return (0);
}
|
Java
import java.io.*;
import java.util.Random;
class GeneratingRandomArrays
{
static int RUN = 5 ;
static int lowerBound = 0 ;
static int upperBound = 1000 ;
static int minSize = 10 ;
static int maxSize = 20 ;
public static void main (String[] args) throws IOException
{
Random random = new Random();
for ( int i = 0 ; i < RUN; i++)
{
int size = random.nextInt(maxSize - minSize) +
minSize;
int [] array = new int [size];
System.out.println(size);
for ( int j = 0 ; j < size; j++)
{
int a = random.nextInt(upperBound - lowerBound) +
lowerBound;
System.out.print(a + " " );
}
System.out.println();
}
}
}
|
Python3
import random
RUN = 5 ;
lowerBound = 0 ;
upperBound = 1000 ;
minSize = 10 ;
maxSize = 20 ;
if __name__ = = '__main__' :
for i in range (RUN):
size = random.randrange( 0 , maxSize - minSize) + minSize;
array = [ 0 ] * size;
print (size);
for j in range (size):
a = random.randrange( 0 , upperBound - lowerBound) + lowerBound;
print (a, end = " " );
print ();
|
C#
using System;
class GeneratingRandomArrays
{
static int RUN = 5;
static int lowerBound = 0;
static int upperBound = 1000;
static int minSize = 10;
static int maxSize = 20;
public static void Main(String[] args)
{
Random random = new Random();
for ( int i = 0; i < RUN; i++)
{
int size = random.Next(maxSize - minSize) +
minSize;
int [] array = new int [size];
Console.WriteLine(size);
for ( int j = 0; j < size; j++)
{
int a = random.Next(upperBound - lowerBound) +
lowerBound;
Console.Write(a + " " );
}
Console.WriteLine();
}
}
}
|
Javascript
<script>
let RUN = 5;
let lowerBound = 0;
let upperBound = 1000;
let minSize = 10;
let maxSize = 20;
for (let i = 0; i < RUN; i++)
{
let size = Math.floor(Math.random() *
(maxSize - minSize)) + minSize;
let array = new Array(size);
document.write(size+ "<br>" );
for (let j = 0; j < size; j++)
{
let a = Math.floor(Math.random() *
(upperBound - lowerBound)) +
lowerBound;
document.write(a + " " );
}
document.write( "<br>" );
}
</script>
|
C++
#include<bits/stdc++.h>
using namespace std;
#define RUN 3
#define MAX 100000
#define MAXROW 10
#define MAXCOL 10
int main()
{
srand ( time (NULL));
for ( int i=1; i<=RUN; i++)
{
int row = 1 + rand () % MAXROW;
int col = 1 + rand () % MAXCOL;
printf ( "%d %d\n" , row, col);
for ( int j=1; j<=row; j++)
{
for ( int k=1; k<=col; k++)
printf ( "%d " , rand () % MAX);
printf ( "\n" );
}
printf ( "\n" );
}
return (0);
}
|
Java
import java.io.*;
import java.util.Random;
class GeneratingRandomMatrix
{
static int RUN = 5 ;
static int lowerBound = 0 ;
static int upperBound = 1000 ;
static int maxColumn = 10 ;
static int minColumn = 1 ;
static int minRow = 1 ;
static int maxRow = 10 ;
public static void main (String[] args) throws IOException
{
Random random = new Random();
for ( int i = 0 ; i < RUN; i++)
{
int row = random.nextInt(maxRow - minRow) +
minRow;
int column = random.nextInt(maxColumn - minColumn) +
minColumn;
int [][] matrix = new int [row][column];
System.out.println(row + " " + column);
for ( int j = 0 ; j < row; j++)
{
for ( int k = 0 ; k < column; k++)
{
int a = random.nextInt(upperBound - lowerBound) +
lowerBound;
System.out.print(a + " " );
}
System.out.println();
}
System.out.println();
}
}
}
|
Python3
import random
RUN = 5 ;
lowerBound = 0 ;
upperBound = 1000 ;
maxColumn = 10 ;
minColumn = 1 ;
minRow = 1 ;
maxRow = 10 ;
if __name__ = = '__main__' :
for i in range (RUN):
row = random.randrange( 0 , maxRow - minRow) + minRow
column = random.randrange( 0 , maxColumn - minColumn) + minColumn
matrix = [[ 0 for i in range (column)] for j in range (row)]
print (row, column)
for j in range (row):
for k in range (column):
a = random.randrange( 0 , upperBound - lowerBound) + lowerBound
print (a ,end = " " )
print ()
print ()
|
C#
using System;
public class GeneratingRandomMatrix
{
static int RUN = 5;
static int lowerBound = 0;
static int upperBound = 1000;
static int maxColumn = 10;
static int minColumn = 1;
static int minRow = 1;
static int maxRow = 10;
public static void Main(String[] args)
{
Random random = new Random();
for ( int i = 0; i < RUN; i++)
{
int row = random.Next(maxRow - minRow) +
minRow;
int column = random.Next(maxColumn - minColumn) +
minColumn;
int [,] matrix = new int [row, column];
Console.WriteLine(row + " " + column);
for ( int j = 0; j < row; j++)
{
for ( int k = 0; k < column; k++)
{
int a = random.Next(upperBound - lowerBound) +
lowerBound;
Console.Write(a + " " );
}
Console.WriteLine();
}
Console.WriteLine();
}
}
}
|
Javascript
const RUN = 5;
const lowerBound = 0;
const upperBound = 1000;
const maxColumn = 10;
const minColumn = 1;
const minRow = 1;
const maxRow = 10;
for (let i = 0; i < RUN; i++) {
const row = Math.floor(Math.random() * (maxRow - minRow + 1)) + minRow;
const column = Math.floor(Math.random() * (maxColumn - minColumn + 1)) + minColumn;
console.log(`${row} ${column}`);
for (let j = 0; j < row; j++) {
let rowString = "" ;
for (let k = 0; k < column; k++) {
const a = Math.floor(Math.random() * (upperBound - lowerBound + 1)) + lowerBound;
rowString += `${a} `;
}
console.log(rowString);
}
console.log( "" );
}
|
Library Functions Used
- rand() Function–
-> Generate random numbers from the range 0 to RAND_MAX (32767)
-> Defined in <stdlib.h>/<cstdlib> header
-> If we want to assign a random number in the range – m to n [n >= m] to a variable var, then use-
var = m + ( rand() % ( n – m + 1 ) );
-> This function is a run-time function. So the values – n, m must be declared before compiling just like we have to declare the size of array before compiling.
-> Call this function every time you want to generate a random number
- time() Function
-> Return the number of seconds from [00:00:00 UTC, January 1, 1970]
-> Defined in <time.h> header
- srand(seed)
-> Generates random number according to the seed passed to it.
-> If this function is not used and we use rand() function then every time we run the program the same random numbers gets generated.
-> To overcome the above limitation, we pass time(NULL) as a seed. Hence srand(time(NULL)) is used to generate random values every time the program is made to run.
-> Always use this at the beginning of the program, i.e- just after int main() {
-> No need to call this function every time you generate a random number
-> Defined in <stdlib.h>/<cstdlib> header
- freopen(“output.txt”, “w”, stdout)
-> Writes (that’s why we passed “w” as the second argument) all the data to output.txt file (The file must be in the same file as the program is in).
-> Used to redirect stdout to a file.
-> If the output.txt file is not created before then it gets created in the same file as the program is in.
- fclose(stdout)
-> Closes the standard output stream file to avoid leaks.
-> Always use this at the end of the program, i.e- just before return(0) in the int main() function.
If you like GeeksforGeeks and would like to contribute, you can also write an article and 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
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 geeks!
Last Updated :
18 Feb, 2023
Like Article
Save Article