Generating Random Sorted Arrays We store the random array elements in an array and then sort it and print it.
CPP
#include<bits/stdc++.h>
using namespace std;
#define RUN 5
#define MAX 100000
#define MAXNUM 100
int main()
{
srand ( time (NULL));
int NUM;
for ( int i=1; i<=RUN; i++)
{
int arr[MAXNUM];
NUM = 1 + rand () % MAXNUM;
printf ("%d\n", NUM);
for ( int j=0; j<NUM; j++)
arr[j] = rand () % MAX;
sort (arr, arr + NUM);
for ( int j=0; j<NUM; j++)
printf ("%d ", arr[j]);
printf ("\n");
}
return (0);
}
|
C#
using System;
namespace TestCasesGenerator {
class Program {
const int RUN = 5;
const int MAX = 100000;
const int MAXNUM = 100;
static void Main( string [] args)
{
Random rand = new Random();
int NUM;
for ( int i = 1; i <= RUN; i++) {
int [] arr = new int [MAXNUM];
NUM = 1 + rand.Next() % MAXNUM;
Console.WriteLine(NUM);
for ( int j = 0; j < NUM; j++)
arr[j] = rand.Next() % MAX;
Array.Sort(arr, 0, NUM);
for ( int j = 0; j < NUM; j++)
Console.Write(arr[j] + " " );
Console.WriteLine();
}
}
}
}
|
Python3
import random
RUN = 5
MAX = 100000
MAXNUM = 100
rand = random.Random()
for i in range ( 1 , RUN + 1 ):
arr = []
NUM = 1 + rand.randint( 0 , MAXNUM - 1 )
print (NUM)
for j in range (NUM):
arr.append(rand.randint( 0 , MAX - 1 ))
arr.sort()
print ( " " .join( str (x) for x in arr))
|
Time complexity : O(N log N)
Space complexity : O(N)
Generating Random Palindromes
- The test case generation plan generates odd as well as even length palindromes.
- The test case generation plan uses one of the most under-rated data structure- Deque
- Since a palindrome is read the same from left as well as right so we simply put the same random characters on both the left side (done using push_front()) and the right side (done using push_back())
CPP
#include<bits/stdc++.h>
using namespace std;
#define RUN 5
#define MAX 25
#define MAXLEN 50
int main()
{
srand ( time (NULL));
deque< char > container;
deque< char >::iterator it;
int LEN;
for ( int i=1; i<=RUN; i++)
{
LEN = 1 + rand () % MAXLEN;
printf ("%d\n", LEN);
if (LEN % 2)
container.push_back( 'a' + rand () % MAX);
for ( int j=1; j<=LEN/2; j++)
{
char ch = 'a' + rand () % MAX;
container.push_back(ch);
container.push_front(ch);
}
for (it=container.begin(); it!=container.end(); ++it)
printf ("%c",*it);
container.clear();
printf ("\n");
}
return (0);
}
|
C#
using System;
using System.Collections.Generic;
namespace Test_Cases_Random_Strings {
class Program {
const int RUN = 5;
const int MAX = 25;
const int MAXLEN = 50;
static void Main( string [] args)
{
Random rand = new Random();
LinkedList< char > container = new LinkedList< char >();
int LEN;
for ( int i = 1; i <= RUN; i++) {
LEN = 1 + rand.Next(MAXLEN);
Console.WriteLine(LEN);
if (LEN % 2 == 1)
container.AddLast(
( char )( 'a' + rand.Next(MAX)));
for ( int j = 1; j <= LEN / 2; j++) {
char ch = ( char )( 'a' + rand.Next(MAX));
container.AddLast(ch);
container.AddFirst(ch);
}
foreach ( char ch in container) Console.Write(ch);
container.Clear();
Console.WriteLine();
}
}
}
}
|
Time complexity : O(N)
Space complexity : O(N)
This article is contributed by Rachit Belwariar. 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. References : – http://spojtoolkit.com/TestCaseGenerator/ Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.