# 多个流

### 使用多个独立流

MATLAB® 软件提供的生成器算法使您能够创建多个独立的随机数流。例如，支持多个独立流的四种生成器类型是组合多递归 (`'mrg32k3a'`)、乘法滞后斐波那契 (`'mlfg6331_64'`)、Philox 4x32 ('`philox4x32_10`') 和 Threefry 4×64 ('`threefry4x64_20`') 生成器。您可以创建保证不重叠的多个独立流，并且已对这些流执行证明流之间值的（伪）独立性的测试。有关支持多个流的生成器算法的详细信息，请参阅创建和控制随机数流中的生成器算法表。

`RandStream.create` 函数使您能够创建三个流，这些流具有相同的生成器算法和种子值，但在统计上是独立的。

`[s1,s2,s3] = RandStream.create('mlfg6331_64','NumStreams',3)`
```s1 = mlfg6331_64 random stream StreamIndex: 1 NumStreams: 3 Seed: 0 NormalTransform: Ziggurat ```
```s2 = mlfg6331_64 random stream StreamIndex: 2 NumStreams: 3 Seed: 0 NormalTransform: Ziggurat ```
```s3 = mlfg6331_64 random stream StreamIndex: 3 NumStreams: 3 Seed: 0 NormalTransform: Ziggurat ```

```r1 = rand(s1,100000,1); r2 = rand(s2,100000,1); r3 = rand(s3,100000,1); corrcoef([r1,r2,r3])```
```ans = 3×3 1.0000 0.0007 0.0052 0.0007 1.0000 0.0000 0.0052 0.0000 1.0000 ```

```streamNum = 256; streamId = 4; s4 = RandStream.create('mlfg6331_64','NumStreams',streamNum,'StreamIndices',streamId)```
```s4 = mlfg6331_64 random stream StreamIndex: 4 NumStreams: 256 Seed: 0 NormalTransform: Ziggurat ```

### 使用种子获得不同结果

`s1 = RandStream('mt19937ar','Seed',1)`
```s1 = mt19937ar random stream Seed: 1 NormalTransform: Ziggurat ```
`s2 = RandStream('mt19937ar','Seed',2)`
```s2 = mt19937ar random stream Seed: 2 NormalTransform: Ziggurat ```

`r1 = rand(s1,100000,1);`

`r2 = rand(s2,100000,1);`

`corrcoef([r1,r2])`
```ans = 2×2 1.0000 0.0030 0.0030 1.0000 ```

### 使用子流获得不同结果

```defaultStream = RandStream('mlfg6331_64'); RandStream.setGlobalStream(defaultStream) for i = 1:5 defaultStream.Substream = i; z = rand(1,i) end```
```z = 0.6986 ```
```z = 1×2 0.9230 0.2489 ```
```z = 1×3 0.0261 0.2530 0.0737 ```
```z = 1×4 0.3220 0.7405 0.1983 0.1052 ```
```z = 1×5 0.2067 0.2417 0.9777 0.5970 0.4187 ```

```for i = 6:10 defaultStream.Substream = i; z = rand(1,11-i) end```
```z = 1×5 0.2650 0.8229 0.2479 0.0247 0.4581 ```
```z = 1×4 0.3963 0.7445 0.7734 0.9113 ```
```z = 1×3 0.2758 0.3662 0.7979 ```
```z = 1×2 0.6814 0.5150 ```
```z = 0.5247 ```

```defaultStream.Substream = 6; z = rand(1,5)```
```z = 1×5 0.2650 0.8229 0.2479 0.0247 0.4581 ```