% RandIntegers.m
% Test 2 different methods for generating random integers
% generated within a given interval [lower, upper].
% K. Ming Leung, version 1: 2008.04.01

clear;
lower = 3; upper = 7;
numberPts = (upper-lower+1)*1000;

subplot(1,2,1);
%%% correct calculation
RandInts1 = floor(lower+rand(1,numberPts)*(upper+1-lower));
Distribution1 = hist(RandInts1,lower:upper)
hist(RandInts1,lower:upper)
xlabel('Numbers Generated');
ylabel('Frequency Count');
title('Correct Method');

subplot(1,2,2);
%%% incorrect calculation
RandInts2 = round(lower+rand(1,numberPts)*(upper-lower));
Distribution2 = hist(RandInts2,lower:upper)
hist(RandInts2,lower:upper)
xlabel('Numbers Generated');
ylabel('Frequency Count');
title('Wrong Method');

figure;

lower = 3; upper = 9;
numberPts = 20;
rand('state',3);
RI = floor(lower+rand(numberPts,1)*(upper+1-lower));
RI5 = RI; RI5(RI==5) = 55;
Index5 = find(RI==5); RI55 = RI; RI55(Index5) = 55;
RI11 = RI; RI11((RI==5)|(RI==7)) = 11;
RI0 = RI; RI0((6<=RI)&(RI<=8)) = 0;
RIminus = RI; RIminus((RI==3)|((6<=RI)&(RI<=8))) = -1;

disp([RI RI5 RI55 RI11 RI0 RIminus]);

% meanValue = 1500; sigmaValue = 400;
% randn('state',2);
% TotalWeights = sigmaValue*randn(1,300)+meanValue;
% TotalWeights(13) = 110; TotalWeights(113) = 4900; 
% TotalWeights(201) = 3350; 
% xlswrite('TotalWeightsData',TotalWeights','DataSheet1','C4:C303');
TotalWeights = xlsread('TotalWeightsData','DataSheet1','C4:C303');
mu = mean(TotalWeights),
sigma = std(TotalWeights), sigma3 = 3*sigma
BadDataL = (TotalWeights < mu - sigma3 | TotalWeights > mu + sigma3);
Outliers = TotalWeights(BadDataL),
TotalWeights(BadDataL) = [];
