0% found this document useful (1 vote)
64 views

Program

This document simulates the spread of a computer virus over time. It initializes parameters like the maximum time, infection probability, number of computers, and number of infections before a computer is fixed. It then runs a simulation for multiple trials, tracking the time until all computers are cleared, the probability a computer is infected at least once, and the average number of infections. The output provides the expected values for each metric based on the simulation results.

Uploaded by

2711323
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
64 views

Program

This document simulates the spread of a computer virus over time. It initializes parameters like the maximum time, infection probability, number of computers, and number of infections before a computer is fixed. It then runs a simulation for multiple trials, tracking the time until all computers are cleared, the probability a computer is infected at least once, and the average number of infections. The output provides the expected values for each metric based on the simulation results.

Uploaded by

2711323
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Tmax = 100000;

p = 0.10;
Nc = 20;
Fix = 5;
N = 100000;

Time_to_clear = zeros(N,1);
All_infected = zeros(N,1);
Num_infected= zeros(N,1);

for k=1:N;
T = 0 ;
Infected_comps = [1
zeros(Nc-i , 1)];

Infected_at_least_once = Infected_comps;
Ninfected = sum(Infected_comps);

while Ninfected > 0 8: T < Tmax;


T = T+1;
P_infection = 1-(1-p)^Ninfected;

Infected_comps = max( Infected_comps, (rand(Nc,1) < P_infection) );


Infected_at_least_once = max(Infected_at_least_once, Infected_comps);

Compfix = 1; Ncompfix = 0;
while (Campfix <= Nc) Kc (Ncompfix < Fix);
if Infected_comps(Compfix)==1;
Infected_comps (Compfix) =0;
Ncompfix = Ncompfix+1;
end;
Compfix = Campfix + 1;
end;
Ninfected = sum(lnfected_comps);
end;

Time_to_clear(k) = T;

All_infected(k) ( sum(Infected_at_least_once)==Nc );
Num_infected(k) ( sum(Infected_at_least_once) );
end;

disp([’The expected time until all computers are fixed is ’ , ’approximately ’,


num2str( mean(Time_to_clear) ), ’ days’]);

disp([’The probability that each computer gets infected at least ’ ,’once is


approximately ’, num2str( mean(All_infected) )]);

disp([’The expected number of computers that get infected is ’ , ’approximately ’,


num2str( mean(Num_infected) )]);

-----------------------------------------------------------------------------------
--------------------------------------------------
output
-----------------------------------------------------------------------------------
--------------------------------------------------
The expected time until all computers are fixed is approximately 73 days
The probability that each computer gets infected at least once is approximately
0.0012
The expected number of computers that get infected is approximately 2.98

You might also like