Hi all
i have an interesting project that i have been working on. i intended to
set this as a first year programming problem but then changed my mind
since i thought that it might be too difficult for them to program.
the problem is as follows:
You have been approached by a local casino in order to
investigate the performance of one of their slot machines.
The slot machine consists of three independently operating
reels on which one of six different symbols can appear. The
symbols are hearts (H), diamonds (D), spades (S), clubs (C) a
joker (J) and a castle (Ca). People bet 1 unit at a time in
order to play the game and are paid out according to the
arrangement of the three reel symbols.
Each reel consists of a number of different tiles. For example, the
first reel contains 40 tiles. The second has 40 tiles and the third
has 50 tiles. Each time the game is played each of the reels spin
such that 1 of the 40 tiles (for reel 1 and similarly for the other
reels) will appear. The number of tiles that occur on each of the
reels are shown below: (i havn't included these but they are in the
code below: ie)
* I've written three functions that will solve the above problem. the
code is attached below. the code is very fast but i would like to
improve the speed by not utilizing loops. is that possible?
* another question? in the function called GAMBLING, i use the
following :
a<-COUNTER(reelpic,nreels,countcombs,payoffcombs,payoff,bet)
countcombs<-a$countcombs
payoff<-a$payoff
payoffvec[i]<-payoff
in order to count up the number of times each of the times we get
each of the payoff symbol combinations (ie H, HH, HHH, D, DD, DDD,
... J,JJ,JJJ, Ca, CaCa, CaCaCa). countcombs is a vector that
contains the counts of the various payoff symbol combinations. the
function COUNTER calculates these values (ie basically just adds
one if a particular combination occurs) and it is attached as a
list object in COUNTER. is there a way of declaring "PUBLIC
VARIABLES" (as allowed by VBA) that allows one to know the value of
different variables as caclulated in different functions without
using the method that i used. ie. using the value
countcombs<-a$countcombs after calling
a<-COUNTER(reelpic,nreels,countcombs,payoffcombs,payoff,bet)
* Another question: in the function RUNDIST i used the following 2
lines
zrung<-GAMBLING(nsim=150)
z[p]<-cumsum(zrung$payoffvec)[150]
Initially the second of these lines was set as
z[p]<-cumsum(zrung$payoffvec)[nsim]
which caused an error. why does this happen?
Sorry for the extremely long email but any help would be much
appreciated.
regards
Allan
the following functions are attached:
* GAMBLING- this function simulates the basic game as stated above
* COUNTER - this function calculates the number of times each of the
various payoff combinations occur
* FORMATCOMBSPDF - this function creates a table of the simulated pdf
of the payoff combinations
* RUNDIST - allows one to generate a distribution of a gamblers total
payoff after playing the game 150 times.
####################################################################################
#GAMBLING- this function simulates the basic game as stated above
GAMBLING<-function(nsim)
{
#denote hearts =1,diamonds =2,spades =3,clubs =4, joker=5, castle = 6
time1<-Sys.time()
#the upper level of the pdf
uplimit1<-c(0.14,0.24,0.30,0.40,0.50,1)
uplimit2<-c(0.14,0.30,0.44,0.50,0.56,1)
uplimit3<-c(0.16,0.30,0.36,0.50,0.56,1)
payoffcombs<-matrix(c(2,6,34,2,8,48,2,13,211,2,21,127,2,19,296,0,0,0),nrow=18,ncol=1)
bet<-1
t<-matrix(data=0,nrow=length(uplimit1),ncol=3)
reelpic<-matrix(data=0,nrow=1,ncol=3)
countcombs<-matrix(data=0,nrow=length(payoffcombs),ncol=1)
payoffvec<-matrix(data=0,nrow=nsim,ncol=1)
nreels<-3
payoff<-0
uplimit<-matrix(c(uplimit1,uplimit2,uplimit3),ncol=length(uplimit1),nrow=nreels,byrow=TRUE)
#the loop over the number the simulation counter
for (i in 1:nsim)
{
#the loop over the number the reels
for (j in 1:nreels)
{
unif<-runif(n=1, min=0, max=1)
#print(unif)
#the loop over the number of prob categories
for (k in 1:length(uplimit1))
{
if (unif<=uplimit[j,k])
{
#counts up the number of times we get
#each of the symbols
t[k,j]=t[k,j]+1
#the reel picture generated
reelpic[1,j]<-k
break
}# endif
}# next k
}# next j
a<-COUNTER(reelpic,nreels,countcombs,payoffcombs,payoff,bet)
countcombs<-a$countcombs
payoff<-a$payoff
payoffvec[i]<-payoff
}# next i
totals<-apply(t,2,sum)
pdf<-sweep(t,2,totals,"/")
combspdf<-sweep(countcombs,1,nsim,"/")
b<-FORMATCOMBSPDF(combspdf)
time2<-Sys.time()
timer<-time2-time1
aa<-paste("THE OUTPUT LIST: $COMBSPDF, $payoff, $payoffvec,
$timer,$hist, $output")
list(COMBSPDF=b$COMBSPDF,payoff=cumsum(payoffvec)[nsim],payoffvec=payoffvec,timer=timer,output=aa)
}
####################################################################################
#COUNTER - this function calculates the number of times each of the
various payoff combinations occur
COUNTER<-function(reelpic,nreels,countcombs,payoffcombs,payoff,bet)
{
rowcounter<-1
for (ci in 1:(nreels-1))
{
ifelse (reelpic[1,ci+1]==reelpic[1,ci],rowcounter<- 1 +
rowcounter,break)
}# nextci
tile1<-reelpic[1,1]
countcombs[3*tile1+rowcounter-3]<-1+countcombs[3*tile1+rowcounter-3]
payoff<-payoffcombs[3*tile1+rowcounter-3]-bet
list(countcombs=countcombs,payoff=payoff)
}
####################################################################################
#FORMATCOMBSPDF - this function creates a table of the simulated pdf of
the payoff combinations
FORMATCOMBSPDF<-function(combspdf)
{
COMBSPDF<-as.data.frame(combspdf)
names(COMBSPDF)[1] <- "PROBLEVELS"
row.names(COMBSPDF)[1] <- "H"
row.names(COMBSPDF)[2] <- "HH"
row.names(COMBSPDF)[3] <- "HHH"
row.names(COMBSPDF)[4] <- "D"
row.names(COMBSPDF)[5] <- "DD"
row.names(COMBSPDF)[6] <- "DDD"
row.names(COMBSPDF)[7] <- "S"
row.names(COMBSPDF)[8] <- "SS"
row.names(COMBSPDF)[9] <- "SSS"
row.names(COMBSPDF)[10] <- "C"
row.names(COMBSPDF)[11] <- "CC"
row.names(COMBSPDF)[12] <- "CCC"
row.names(COMBSPDF)[13] <- "J"
row.names(COMBSPDF)[14] <- "JJ"
row.names(COMBSPDF)[15] <- "JJJ"
row.names(COMBSPDF)[16] <- "Ca"
row.names(COMBSPDF)[17] <- "CaCa"
row.names(COMBSPDF)[18] <- "CaCaCa"
list(COMBSPDF=COMBSPDF)
}
####################################################################################
#RUNDIST - allows one to generate a distribution of a gamblers total
payoff after playing the game 150 times.
RUNDIST<-function(nruns)
{
stime<-Sys.time()
z<-matrix(data=0,nrow=nruns,ncol=1)
for (p in 1:nruns)
{
zrung<-GAMBLING(nsim=150)
z[p]<-cumsum(zrung$payoffvec)[150]
}
par(mfrow=c(1,2))
#plot(z,type="l",main="Accumulated Payoffs",xlab="",font.main=6)
hist(z,prob=TRUE,main="Histogram of the Accumulated
Payoffs",xlab="Accumulated Payoffs",font.main=6)
lines(density(z),col="blue")
boxplot(z,main="Box and Whisker Plot of the Accumulated
Payoffs",font.main=6)
stem(z)
ftime<-Sys.time()
out<-paste(" $cumpayoffs , $timer , $output ")
list(cumpayoffs=z,timer=ftime-stime,output=out)
}
[[alternative HTML version deleted]]