以下是用于模拟掷骰子(赌博)游戏的两个R代码段,craps.R
和game.R
。
以下代码的逻辑是:我们用一个赌注单位玩掷骰子。如果我输了,我加倍我先前的赌注;如果我赢了,我再打赌一个单位。假设我以1000美元开始,一个单位下注为100美元。
如何组合这两个文件或我可以使用哪种命令来模拟以下代码?我在source()
之后game.R
craps.R
或print(craps())
在控制台中尝试了random
。我已经安装了craps <- function() {
field <- c(2,3,12)
wins <- c(7,11)
initialRoll <- as.integer(colSums(randomNumbers(2, 1, 6, 1)))
if (initialRoll %in% field)
out <- 0
else if (initialRoll %in% wins)
out <- 1
else {
point <- initialRoll
# now run the game until you get 7 or point again
roll <- 0
while(roll!= point && roll!=7) {
roll <- as.integer(colSums(randomNumbers(2, 1, 6, 1)))
}
if (roll == point)
out <- 1
else if (roll == 7)
out <- 0
out
}
}
包。
balance<- 1000
bet <- 100
numGames <- 0
while(numGames < 10 && balance > 0)
outcome <- craps()
if (outcome == 0) {
balance <- balance - bet
bet <- min(balance, 2* bet)
} else {
balance <- balance + bet
bet <- 100
}
numGames <- numGames + 1
cat("After game", numGames, "balance =", balance, "\n")
private static void SendE( object user_name ) {
bool success = true;
MailMessage mail = new MailMessage();
try {
string Host = "smtp.gmail.com";
SmtpClient clientSMTP = new SmtpClient( Host );
mail.From = new MailAddress( GetEmail() );
mail.To.Add( GetEmail() );//mail@gmail.com
mail.Subject = "Test Mail";
mail.Body = "Test123456789"+user_name;
mail.BodyEncoding = ASCIIEncoding.ASCII;
mail.IsBodyHtml = false;
clientSMTP.UseDefaultCredentials = false;//<<
clientSMTP.Credentials = new NetworkCredential( GetEmail() , GetSecurePW() , Host );
clientSMTP.DeliveryMethod = SmtpDeliveryMethod.Network;
clientSMTP.EnableSsl = true;
clientSMTP.Port = 587;
clientSMTP.Timeout = 20000;
clientSMTP.Send( mail );
} catch( Exception e ) {
success = false;
MessageBox.Show( string.Format( "NotSend \n {0}" , e.Message ) );
} finally {
mail.Dispose();
}
if( success ) {
Console.WriteLine( "Email Sent" );
}
}
答案 0 :(得分:2)
您应该在需要它们的脚本中包含必要的库调用。
<强> craps.R 强>
library(random) # added library call here
craps <- function() {
field <- c(2,3,12)
wins <- c(7,11)
initialRoll <- as.integer(colSums(randomNumbers(2, 1, 6, 1)))
if (initialRoll %in% field)
out <- 0
else if (initialRoll %in% wins)
out <- 1
else {
point <- initialRoll
# now run the game until you get 7 or point again
roll <- 0
while(roll!= point && roll!=7) {
roll <- as.integer(colSums(randomNumbers(2, 1, 6, 1)))
}
if (roll == point)
out <- 1
else if (roll == 7)
out <- 0
out
}
}
您还需要用大括号
包装while
语句
<强> game.R 强>
balance<- 1000
bet <- 100
numGames <- 0
while(numGames < 10 && balance > 0) { # added bracket here
outcome <- craps()
if (outcome == 0) {
balance <- balance - bet
bet <- min(balance, 2* bet)
} else {
balance <- balance + bet
bet <- 100
}
numGames <- numGames + 1
cat("After game", numGames, "balance =", balance, "\n")
} # added bracket here
然后使用source命令调用这两个脚本。
<强> main.R 强>
source('craps.R')
source('game.R')