用图解模拟n个矩阵样本

时间:2015-11-04 07:58:58

标签: r matrix

我想创建一个允许用户选择nxn矩阵的函数,用0和1的样本填充它,查找TIC TAC TOE“wins”(连续的所有1或0,col或diag) ),并绘制一个基于随机样本的获胜解决方案概率的图表。我希望用户能够运行任意数量的模拟。

我已经有了创建单个矩阵的代码

ranmat = function(nrow,ncol){
matrix(sample(c(0,1),replace=T,size=nrow*ncol),nrow=nrow)

我也有关于如何检查胜利的想法,但我不知道如何运行模拟 n 次。

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找一个for循环?我认为有很多方法可以做你想做的事。

# 1st option: you might want to call your function n times:
# with l,k and n being integer values (n is the number of simulation)
for(i in 1:n) ranmat(nrow=k,ncol=l) 

# 2nd option: you tell your function how often you want to simulate:
ranmat = function(nrow,ncol,nsimult){
    for(i in 1:nsimult){
        # your simulations
    }
    return(result) # with result being an appropriate way to return the outcome of your simulations
}
ranmat(nrow=k,ncol=l,nsimult=n) #call to your function with k,l and n being integer values