SUNY Geneseo Department of Mathematics
Friday, April 3
Math 384
Spring 2015
Prof. Doug Baldwin
if mod(a,2) == 1
numeratorValues = 2 : 2 : a-1;
denominatorValues = 3 : 2 : a;
else
numeratorValues = [ 3 : 2 : a-1, pi/4 ];
denominatorValues = 4 : 2 : a;
end
n = prod( denominatorValues ) / prod( numeratorValues );
for i = 1 : nSamples
while samples(i) > coinFlip
samples(i) = rand * pi/2
p = ...
coinFlip = rand * n
nSamples = 509;
samples = zeros( 1, nSamples );
i = 1; % Index of sample being generated
while i <= nSamples % Rejection sample until list full
x = rand * pi / 2;
if rand < cos(x)^a
samples(i) = x;
i = i + 1;
end
end
sampleCosine
uses the precomputed samples and the “next sample” value:x = sampler.samples( sampler.next );
sampler.next = mod( sampler.next, sampler.maxSamples ) + 1;