This is a pascal program so save it in .pas file
Program TWODHISTOGRAM;
{ Author : VENKAT SRI CHAITYA THOTA
Date : 11/14/2013
Course Name: CSC 540 Graduate Research Seminar
Description: The program illustrates the behavior of the Pascal random number generator by throwing random numbers between 20001 and 380000 (inclusive). In the first part, the results are binned into into 10 equal histogram bins and the bin
Frequencies are written into an output file with extension .csv after each simulation run. The .csv file can be directly opened with excel which will be used as a graphing program.
In the second part, the results are binned into 36 equal histogram bins and written into an output .csv file in the form of a 6×6 matrix after each simulation run. The resultant frequencies will then used to plot 2D histogram.
The code is developed in such a way that it takes two runtime inputs from the user so that the same code can be re-used to generate all the required results. The inputs are:
1. The bins to generate and
2. The runs to do
Output: The output of bin frequencies is displayed in Freq_output.csv which will be stored in the FPC directory.
}
{Declaring the variables and their types}
{Note: The number of runs and the width of the bin are declared as longint as they could fall beyond the limit of the integer}
Var
numbins : Integer;{To hold the user input-Number of bins}
numruns : Longint;{To hold the user input-Number of runs}
binwidth : Longint;{To hold the calculated Width of the Bins based on the user inputs}
randnumb : Longint;{To hold Random number from ranging from 20001 to 380000}
loopvar1 : Longint;{Used as a Loop variable}
loopvar2 : Longint;{Used as a Loop variable}
indexval : Longint;{Used as a Loop variable for calculation}
quotient : Integer;{Bin to which the random number belongs to – ranges from 1 to numbins}
count : Array[1..36] of Integer;{Array to store the Bin frequencies for normal histogram}
binfreq : Array[1..6,1..6] of Integer;{Array to store the Bin frequencies for the 2D histogram}
output : Text;{Variable to assign the output file}
Begin
{Prompt for the user inputs at runtime and read the same: the number of bins to generate and the number of runs}
writeln(‘Enter the number of bins:’);
readln(numbins);
writeln(‘Enter the number of runs:’);
readln(numruns);
{Simulation for 10 bins}
if numbins = 10 then begin
{Calculate width of the bin based on the user inputs}
binwidth := trunc(360000/numbins);
{The below code initializes the bin frequencies to zero}
For loopvar1 := 1 to 36 do begin
count[loopvar1] := 0;
End; {Ends the initialization for loop}
{To generate random numbers later in the code}
randomize;
{Running the simulation for numruns}
{Loop for number of runs}
For loopvar1 := 1 to numruns do begin
randnumb := random(360000)+ 20001;{Note: random function with an argument ‘a’ creates values from 0 to a-1}
quotient := (trunc( (randnumb – 20001)/binwidth ))+1;{quotient ranges from 1 to numbins and is the bin to which the random number randnumb belongs to}
count[quotient] := count[quotient]+1;{Incrementing the frequency of the bin to which randnumb belongs to by 1}
End; { Ends the numruns for loop}
{Generating the output file}
Assign(output,’Frequency_output.CSV’);{Creating an output file with name: Frequency_output.CSV . Please note the output format is .csv which can be directly imported to Excel and used as the graphing program later}
Rewrite(output);
For loopvar1 := 1 to numbins do begin {Writing the bin frequencies into the output file}
Writeln(output, count[loopvar1]);
End;
Writeln(‘The bin frequencies are displayed in the frequency_output.csv file in the FPC directory’);
End;{Ends If}
{Simulation for 36 bins}
if numbins = 36 then begin
{Calculate width of the bin based on the user inputs}
binwidth := trunc(360000/numbins);
{The below code initializes the bin frequencies to zero}
For loopvar1 := 1 to 36 do begin
count[loopvar1] := 0;
End;
{To generate random numbers later in the code}
randomize;
{Running the simulation for numruns runs}
{Loop for number of runs}
For loopvar1 := 1 to numruns do begin
randnumb := random(360000)+ 20001;{Note: random function with an argument ‘a’ creates values from 0 to a-1}
quotient := (trunc( (randnumb – 20001)/binwidth ))+1;{quotient ranges from 1 to numbins and is the bin to which the random number randnumb belongs to}
count[quotient] := count[quotient]+1;{Incrementing the frequency of the bin to which randnumb belongs to by 1}
End; { Ends the numruns for loop}
{Code for creating the 6×6 Matrix of frequencies for 2D histogram}
For loopvar1 := 1 to 6 Do Begin
For loopvar2 := 1 To 6 Do Begin
indexval:= loopvar2 + (6*(loopvar1-1));
binfreq[loopvar1,loopvar2]:= count[indexval];{For example: binfreq[1,1] = count[1], binfreq[1,6] = count[6], binfreq[2,1] = count[7], binfreq[2,6] = count[12] and so on}
End;{End of Inner for loop}
End; {End of Outer for loop}
{Generating the output file}
Assign(output,’Frequency_output.CSV’);{Creating an output file with name: Frequency_output.csv. Please note the output format is .csv which can be directly imported to Excel and used as the graphing program later}
Rewrite(output);
For loopvar1 := 1 to 6 Do Begin {Writing the 6×6 matrix of frequencies into the output file}
Writeln(output, binfreq[1,loopvar1],’,’,binfreq[2,loopvar1],’,’,binfreq[3,loopvar1],’,’,binfreq[4,loopvar1],’,’,binfreq[5,loopvar1],’,’,binfreq[6,loopvar1]);
End;
Writeln(‘The bin frequencies are displayed in the freq_output.csv file in the FPC directory’);
End;{Ends if}
Close(output);
Readln;
End.
{End of the program}