function [worsedata, indaffected] = simRubbish(cleandata, rrubbish, rangerubbish)
% The values in some columns of the matrix cleandata is replaced by
% uniformly distributed random numbers drawn from the given range.
%
% Input:
%
%   cleandata - the clean data to be made bad; columns are observations,
%               rows are variables.
%
%   rrubbish  - number of columns to corrupt. The columns are
%               picked randomly.
%
%   rangerubbish - a two-vector that gives the lower and upper
%               limit of the random numbers generated, i.e., they
%               will be drawn from the interval [rangerubbish(1), 
%               rangerubbish(2)]
%
%
% Output:
%
%   worsedata   - the mangled data matrix.
%
%   indaffected - indices of the affected columns of data

worsedata = cleandata;

indaffected = 1:size(cleandata,2);
indaffected = indaffected(randperm(length(indaffected)));
indaffected = indaffected(1:round(rrubbish*numel(indaffected)));
worsedata(:,indaffected) = ones(size(cleandata,1),numel(indaffected)) * rangerubbish(1) ...
    + rand(size(cleandata,1),numel(indaffected)) * (rangerubbish(2)-rangerubbish(1));
