# 伪随机细胞自动机

14

## 介绍

• 如果为`min(a,b) == 0`，则的新状态`b``max(a,b)`
• 如果为`min(a,b) == 1`，则`b`从中随机选择的新状态`{0,1}`

``````1
11
101
1111
11001
101011
1111111
10001001
110011011
1010111101
``````

## 例

``````[1]

[1,1]
v
[1,0,1]

[1,1,1,1]
v v v
[1,1,0,0,1]
v
[1,1,1,0,1,1]
v v   v
[1,0,0,1,1,1,1]
``````

## 测试用例

``````Inputs: 10 [0]
Output:
1
11
101
1111
10001
110011
1010101
11111111
100000001
1100000011
``````

``````Inputs: 6 [1,1]
Output:
1
11
111
1111
11111
111111
``````

``````Inputs: 10 [0,0,1]
Output:
1
11
101
1111
10101
111111
1010011
11110101
101011111
1111101001

Inputs: 10 [1,0,0,1]
Output:
1
11
111
1001
11011
111111
1001101
11010111
111111101
1011001111

Inputs: 15 [1,1,1,0,0,0]
Output:
1
11
111
1111
10001
110011
1110111
11011001
111111011
1100011111
11100100011
111101100101
1001111101111
11011000111111
101101001011101
``````

3

# Pyth，33个字节

``````jjLk.u++1m?hSde=.<Q1sd.:N2 1tvz]1
``````

### 说明：

``````jjLk.u++1m?hSde=.<Q1sd.:N2 1tvz]1  implicit: Q = input list
.u                      tvz]1  reduce N=[1] input-1 times by applying
.:N2           all substrings of length 2
m                           map each d of ^ to:
?hSd                         if min(d) = 0 then:
=.<Q1                     rotate Q by one
e                          and use the last element
sd                 else use sum(d) (=max(d))
++1                  1         add a 1 at the front and the back
.u gives all intermediate results
jLk                               join these lists to strings
j                                  print each string on a line
``````

7

# 视网膜，139字节

``````^.
1

00:0 01:1 10:1 11:
(m`^(..)((\S*)(?<=0) .*)
\$1\$3#\$1!\$2
+m`(?<=^(?<-2>.)*(..).*?#(.)*.)\d!(.)(.*\1:)(.)(\d*)
\$5\$3!\$4\$6\$5
)`!0
0
.+
<empty>
``````

``````0000000000 1001
``````

``````1000000000
1100000000
1110000000
1001000000
1101100000
1111110000
1001101000
1101011100
1111111010
1011001111
``````

3

# Python中，142 135 132 131字节

### 133 132 131字节版本

``````f=input;n=f();L=f()*n*n;r=[1];i=1
while i<=n:print r;r=[L.pop(0)if r[x-1]&r[x]else r[x-1]+r[x]for x in range(1,i)];r=[1]+r+[1];i+=1
``````

### 135字节版本

``````f=input;n=f();L=f()*n**2;r=[1];i=1
while i<=n:print r;r=[L.pop(0)if r[x-1]+r[x]>1 else r[x-1]+r[x]for x in range(1,i)];r=[1]+r+[1];i+=1
``````

`min(r[x-1],r[x])->r[x-1]+r[x]>1`

`max(r[x-1],r[x])->r[x-1]+r[x]`

### 142字节版本

``````f=input;n=f();L=f()*n**2;r=[1];i=1
while i<=n:print r;r=[L.pop(0)if min(r[x-1],r[x])else max(r[x-1],r[x])for x in range(1,i)];r=[1]+r+[1];i+=1
``````

``````10 # This is input
[0] # This is input
[1] <- First output row
[1, 1]
[1, 0, 1]
[1, 1, 1, 1]
[1, 0, 0, 0, 1]
[1, 1, 0, 0, 1, 1]
[1, 0, 1, 0, 1, 0, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 0, 0, 0, 0, 0, 0, 0, 1]
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1]
``````

### 现在简要说明其工作原理：

``````f=input;n=f();L=f()*n*n;r=[1];i=1 First we define the input() function as f
for saving bytes as we have to call it twice.
Then L is defined as a list made of the
pseudorandom numbers in their order *many* times
(were *many* is an upperbound of the canges that
could be done); r as the first row and i as the row
counter.

while i<=n:print r                 A while loop that exits when the nth row has been
calculated and the printing of the actual row.

r=[L.pop(0)if r[x-1]&r[x] else r[x-1]+r[x] for x in range(1,i)];r=[1]+r+[1];i+=1
^           ^                 ^                         ^
|           |                 |Same as max(r[x-1],r[x]) | from 2nd to last element
|           | Same as min(r[x-1],r[x]) (0->False;1->True)
| get random bit from pseudorandom list
``````

1

@Cole幸运的是，不必对程序的其余部分进行任何更改，因为更改仅影响知道一对位的最小值和最大值的方式。
Ioannes 2015年

1

n ** 2-> n * n是否有效？
lirtosiast 2015年

@托马斯你是对的！
Ioannes 2015年

2

# MATLAB，146 143 138

（也可以在线使用Octave，但是您需要登录以将功能保存到文件中）。

``````function o=c(n,L);o=zeros(n);o(:,1)=1;for i=2:n;for j=2:i;a=o(i-1,j-1);b=o(i-1,j);c=a|b;d=a&b;c(d)=L(d);L=circshift(L,-d);o(i,j)=c;end;end
``````

``````function o=c(n,L)
%Create the initial array - an n x n square with the first column made of 1's
o=zeros(n);o(:,1)=1;
%For each row (starting with the second, as the first is done already)
for i=2:n;
%For each column in that row, again starting with the second as the first is done
for j=2:i;
%Extract the current and previous elements in the row above
a=o(i-1,j-1); %(previous)
b=o(i-1,j);   %(current)
%Assume that min()==0, so set c to max();
c=a|b;
%Now check if min()==1
d=a&b;
%If so, set c to L(1)
c(d)=L(d);
%Rotate L around only if min()==1
L=circshift(L,-d);
%And store c back to the output matrix
o(i,j)=c;
end;
end
``````

1

``````j[_]o l=(l,o)
j(a:u@(b:c))o q@(l:m)|a*b==0=j u(o++[a+b])q|1<2=j u(o++[l])m
k(r,a)=fmap((1:).(++[1]))\$j a[]r
n%l=map snd\$take n\$iterate k(cycle l,[1])
``````

`%`返回位列表的列表。用法示例：

``````> 10 % [1,0,0,1]
[[1],[1,1],[1,1,1],[1,0,0,1],[1,1,0,1,1],[1,1,1,1,1,1],[1,0,0,1,1,0,1],[1,1,0,1,0,1,1,1],[1,1,1,1,1,1,1,0,1],[1,0,1,1,0,0,1,1,1,1]]
``````

1

# C＃，152个字节

``````int[,]F(int n,int[]l){
var o=new int[n,n];
for(int y=0,x,i=0,m;y<n;y++)
for(o[y,x=0]=1;x++<y;)
o[y,x]=(m=o[y-1,x-1]+o[y-1,x])<2?m:l[i++%l.Length];
return o;
}
``````

1

# TI-BASIC，106 94 87 86 87字节

``````Prompt N,B
"∟B(1+remainder(𝑛,dim(∟B→u
{1
For(I,1,N
Disp Ans
augment({0},Ans)+augment(Ans,{0
Ans and Ans≠2+seq(u(𝑛-(Ans(X)<2)+2dim(∟B)),X,1,dim(Ans
End
``````

TI-BASIC没有增量运算符，对吗？好吧，确实可以。`u`通常与序列一起使用的方程变量具有晦涩的特征：`u`使用参数调用时，该变量`𝑛`设置为比该参数大一个。条件增量取决于此。（我已经等了很长时间了。）

`augment({0},Ans)+augment(Ans,{0`计算两个相邻元素之和的列表，因此它将返回0、1、2和2的列表。然后魔术就在这条线上：

``````Ans and Ans≠2+seq(u(𝑛-(Ans(X)≠2)+dim(∟B)),X,1,dim(Ans

Ans and                 ;set 0s to 0
Ans≠                    ;set to 0 all sums that equal...
2+
seq(...,X,1,dim(Ans   ;execute for each element of the list
u(                ;return this element in list of bits (looping)
𝑛               ;current location in the list
-(Ans(X)≠2)+    ;subtract 1 if the element isn't 2
2dim(∟B)        ;Add twice the dimension of the list
;(because n<nMin on the first iteration, it's out of the domain
;this prevents an error)
)                      ;set n to one greater than that value
;i.e. increment if element≠2
;Will equal Ans(X) iff Ans(X)=2 and the bit read false
``````

``````Result of above line
n \ u |  0  |  1
0        0     0
``````

``````N=?7
B=?{0,1,0
{1}
{1 1}
{1 0 1}
{1 1 1 1}
{1 1 0 0 1}
{1 1 1 0 1 1}
{1 0 0 1 1 1 1}
Done
``````