MATLAB C or JAVA languages can be used A packet train of le
[MATLAB], [C++], or [JAVA] languages can be used
-----------------------------------------------
A \"packet train\" of length i is a sequence of i consecutive packets (with no gaps or idle slots in between). In a given time slot the probability that a station has a single packet to transmit is p. The probability of no packet in a slot (idle slot) is 1-p. One packet fills one time slot In your program you will need to use a random number generator function to simulate the probability that a slot has a packet or not. For each of three values of p you will plot a histogram showing the relative frequency of packet trains of length i. The horizontal axis of the histograms will be the packet train length (i = 1, 2, 3...) and the vertical axis will be the recorded relative frequency. You will generate histograms for p = 0.2, p = 0.5 and p = 0.9. Also consider the number of time slots to be 100,000. Increase the number of slots if any curve(s) is too bumpy. Record the percentage or relative frequency of each packet train length for the above three packet transmission probabilities. The relative frequency of a packet train of length i is the number of times that packet trains of length i occurs divided by the total number of packet trains of all lengths found in your simulation. Output of your program: Create a histogram showing the relationship of the packet train length and the percentage or relative frequency that specific packet length. Your graph should be such that packet train length is on the X-axis and the Percentage or relative frequency is on the Y-axis. It is possible to find an equation for these simulation curves using basic probability. Extra credit will be awarded if you plot the theoretical curves on the same graph as the experimental curves. They should match pretty closely.Solution
import java.util.Random;
import java.util.Scanner;
public class histo {
static int array[]=new int[100000];
static int occur[]=new int[100000];
public static void main(String args[])
{ Scanner read=new Scanner(System.in);
double p=0;;
while(p<=0|p>1) //ask user input and if it is not in range keep asking
{
System.out.println(\"pls enter p in range 0 to 1\");
try
{
p=read.nextDouble();
}
catch(Exception e)
{
System.out.println(\"error\");
}
}
generate(p);
countoccur();
printHistogram(occur);
read.close();
}
public static void printHistogram(int[] array) {
for (int range = 1; range < array.length; range++) {
if(array[range]!=0)
{
String label = range + \" : \";
System.out.println(label + convertToStars(array[range]));
}}
}
public static String convertToStars(int num) {
StringBuilder builder = new StringBuilder();
for (int j = 0; j < num; j++) {
builder.append(\'*\');
}
return builder.toString();
}
public static void generate(double p)
{
p=p+0.01*100;// e.g if p=0.2 then p=20 out of 100
Random r = new Random();
int Low = 0;
int High = 100;
double Result;
for(int i=0;i<array.length;i++)
{
Result = r.nextInt(High-Low) + Low;
if(Result<p)
{
array[i]=1;
}
}
}
public static void countoccur()
{
int count=0;
int i=0;
for(i=0;i<array.length;i++)
{
while(i<array.length&&array[i]==1)
{
count++;
i++;
}
if(count!=0&&count<occur.length)
{
occur[count]++;
}
}
System.out.println(\"donel\");
}
}
![[MATLAB], [C++], or [JAVA] languages can be used ----------------------------------------------- A \ [MATLAB], [C++], or [JAVA] languages can be used ----------------------------------------------- A \](/WebImages/36/matlab-c-or-java-languages-can-be-used-a-packet-train-of-le-1108986-1761587558-0.webp)
![[MATLAB], [C++], or [JAVA] languages can be used ----------------------------------------------- A \ [MATLAB], [C++], or [JAVA] languages can be used ----------------------------------------------- A \](/WebImages/36/matlab-c-or-java-languages-can-be-used-a-packet-train-of-le-1108986-1761587558-1.webp)
![[MATLAB], [C++], or [JAVA] languages can be used ----------------------------------------------- A \ [MATLAB], [C++], or [JAVA] languages can be used ----------------------------------------------- A \](/WebImages/36/matlab-c-or-java-languages-can-be-used-a-packet-train-of-le-1108986-1761587558-2.webp)