Yes, there is srand(), here is the whole program:
#include <math.h>
#include <time.h>
#include <stdio.h>
void main()
{
int N_REPEAT= 100000;
float P1= 30; // <--- polarizer-1
float P2= 30; // <--- polarizer-2
Init_Setup:;
system("cls");
printf("\Repeat #: 100,000");
printf("\nAngle polarizer P1: "); scanf("%f", &P1);
printf("Angle polarizer P2: "); scanf("%f", &P2);
srand(time(NULL));
int N_MEASURE= 0;
int MATCH= 0;
int MISMATCH= 0;
// relative angle & radian conversion
float REL_P1= 0.0174533* (P1-P2)/2;
float REL_P2= 0.0174533* (P2-P1)/2;
BEGIN:;
int L1= ((rand()%201)/2 < ((cos(REL_P1)*cos(REL_P1))*100)) ? 1:0;
int L2= ((rand()%201)/2 < ((cos(REL_P2)*cos(REL_P2))*100)) ? 1:0;
printf("\n %d%d", L1, L2);
if (L1 == L2) MATCH++; else MISMATCH++;
if (++N_MEASURE < N_REPEAT) goto BEGIN;
printf("\n\n1.)\n--- MALUS LAW INTEGRATION (%.0f,%.0f) ---", P1, P2);
printf("\nMATCH: %d\nMISMATCH: %d", MATCH, MISMATCH);
printf("\nMalus(%d): %.0f%%", abs((int)((P1-P2)/2)), ((cos(REL_P1)*cos(REL_P1))*100));
printf("\n>>> STATISTICAL AVERAGE RESULT: %.2f%%", (float)abs(MATCH-MISMATCH)/(N_MEASURE/100));
// Exact probabilty equation
float T= cos(REL_P1)*cos(REL_P2);
float F= 1.0 - T;
float MCH= (T*T)+(F*F);
float MSM= (T*F)+(F*T);
printf("\n\n\n2.)\n--- MALUS LAW PROBABILTY (%.0f,%.0f) ---", (P1-P2)/2, (P2-P1)/2);
printf("\nMATCH: %.2f%%\nMISMATCH: %.2f%%", MCH*100, MSM*100);
printf("\n>>> EXACT PROBABILTY RESULT: %.2f%%", (MCH-MSM)*100);
printf("\n\nPress any key to repeat.");
getch(); goto Init_Setup;
}
Yes, it gives correct results for any combination of agles of the two polarizers. The more measurements there are, the more results get stable and converge to QM prediction: cos^2(theta).
There is also an exact solution, like calculating probability of matching pairs in two-coins toss sequences.
//Malus's law probability for relative angle theta(P1,P2)
REL_P1= (P1-P2)/2
REL_P2= (P2-P1)/2
T= cos(REL_P1)*cos(REL_P2)
F= 1.0 - T
MACH= (T*T)+(F*F)
MISM= (T*F)+(F*T)
RESULT= (MACH-MISM)*100 = CORRELATION %
Take this experiment for example:
2.)
"Foreground polarizer" P1= +30
"Background polarizer" P2= 0
REL_P1= (30-0)/2= +15
REL_P2= (0-30)/2= -15
T= cos(+15) * cos(-15) = 0.933
F= 1.0 - T = 0.067
MACH= (0.933 * 0.933) + (0.067 * 0.067) = 0.875
MISM= (0.933 * 0.067) + (0.067 * 0.933) = 0.125
RESULT= (0.875 - 0.125) * 100 = 75% correlation = 25% discordance
4.)
"Foreground polarizer" P1= +30
"Background polarizer" P2= -30
REL_P1= (30+30)/2= +30
REL_P2= (-30-30)/2= -30
T= cos(+30) * cos(-30) = 0.75
F= 1.0 - T = 0.25
MACH= (0.75 * 0.75) + (0.25 * 0.25) = 0.625
MISM= (0.75 * 0.25) + (0.25 * 0.75) = 0.375
RESULT= (0.625 - 0.375) * 100 = 25% correlation = 75% discordance