Example #1
0
/*
	Allow NULL context if caller is just doing a single read
*/
int32 psGetPrng(psRandom_t *ctx, unsigned char *bytes, uint32 size)
{
	psRandom_t		lctx;
	
	if (ctx == NULL) {
		psInitPrng(&lctx);
		return readRandomData(&lctx, bytes, size);
	} 
	return readRandomData(ctx, bytes, size);
}
void SortingCompetition::algorithmTester(void){

   int A = 1;
   int stepSize = 10000;

   int max = 10001;
   //number of sorting methods implemented
   int sortingCount = 1;
   //setting number of words in RandomInput.txt
   this->inputSize = A*stepSize;

   //output header to file for output
   ofstream output("SortingAnalysis.txt");
   output<<"N,";
   for(int y = 0;y<sortingCount;y++){
       switch(y){
        case 0:
           output<<"Bubble Sort,";
        case 1:
           output<<"Merge Sort,";
       }
   }
   output<<endl;
   //close output file temporarily while make the randomInput file
   output.close();

   while(A*stepSize<max){
       //function call to make random input file from the words collected from input.txt
       makeRandomFile(A*stepSize,"RandomInput.txt");
       //processing now done on
       setFileName("RandomInput.txt");

       //prepare words2 with RandomInput file instead
       readRandomData(stepSize);
       prepareData();

       //re-open output file and append to the end
       output.open("SortingAnalysis.txt",std::ios_base::app);

       //output input size to output file
       cout<<A*stepSize<<", ";

       //find average of all sorting methods for this specific
       //input size
       for(int i = 0;i<sortingCount;i++){
           double sumRuntime = 0;
           for(int j = 0;j<30;j++){

                //declare 2 time points
                std::chrono::time_point<std::chrono::system_clock> start, end;

                //store current time (now()) in start
                start = std::chrono::system_clock::now();

                //decide which sorting method to use
               switch(i){
                case 0:
                    bubbleSort();
                case 1:
                    mergeSort(0,getWordCount()-1);
               }

                //store time(now()) in end
                end = std::chrono::system_clock::now();

                //get No. of seconds elapsed & output duration
                //need to do this multiple times & get average
                std::chrono::duration<double> elapsed_seconds = end-start;

                sumRuntime += elapsed_seconds.count();
           }//end of 30 runtimes

           //find average runtime
           double average = sumRuntime/30;

           //output to file the average run time and size
           cout<<fixed<<setprecision(9)<<average<<", ";

       }//processed all sorting algorithms

       //increment input size
       A +=1;

       this->inputSize = A*stepSize;

       cout<<endl;

   }

}