## Reproduce Results

### Identical Answers with Pseudorandom Numbers

`GlobalSearch` and `MultiStart` use pseudorandom numbers in choosing start points. Use the same pseudorandom number stream again to:

• Compare various algorithm settings.

• Have an example run repeatably.

• Extend a run, with known initial segment of a previous run.

Both `GlobalSearch` and `MultiStart` use the default random number stream.

### Steps to Take in Reproducing Results

1. Before running your problem, store the current state of the default random number stream:

`stream = rng;`
2. Run your `GlobalSearch` or `MultiStart` problem.

3. Restore the state of the random number stream:

`rng(stream)`
4. If you run your problem again, you get the same result.

### Example: Reproducing a GlobalSearch or MultiStart Result

This example shows how to obtain reproducible results for Find Global or Multiple Local Minima. The example follows the procedure in Steps to Take in Reproducing Results.

1. Store the current state of the default random number stream:

`stream = rng;`
2. Create the `sawtoothxy` function file:

```function f = sawtoothxy(x,y) [t r] = cart2pol(x,y); % change to polar coordinates h = cos(2*t - 1/2)/2 + cos(t) + 2; g = (sin(r) - sin(2*r)/2 + sin(3*r)/3 - sin(4*r)/4 + 4) ... .*r.^2./(r+1); f = g.*h; end```
3. Create the `problem` structure and `GlobalSearch` object:

```problem = createOptimProblem('fmincon',... 'objective',@(x)sawtoothxy(x(1),x(2)),... 'x0',[100,-50],'options',... optimoptions(@fmincon,'Algorithm','sqp')); gs = GlobalSearch('Display','iter');```
4. Run the problem:

```[x,fval] = run(gs,problem) Num Pts Best Current Threshold Local Local Analyzed F-count f(x) Penalty Penalty f(x) exitflag Procedure 0 465 422.9 422.9 2 Initial Point 200 1730 1.547e-015 1.547e-015 1 Stage 1 Local 300 1830 1.547e-015 6.01e+004 1.074 Stage 2 Search 400 1930 1.547e-015 1.47e+005 4.16 Stage 2 Search 500 2030 1.547e-015 2.63e+004 11.84 Stage 2 Search 600 2130 1.547e-015 1.341e+004 30.95 Stage 2 Search 700 2230 1.547e-015 2.562e+004 65.25 Stage 2 Search 800 2330 1.547e-015 5.217e+004 163.8 Stage 2 Search 900 2430 1.547e-015 7.704e+004 409.2 Stage 2 Search 981 2587 1.547e-015 42.24 516.6 7.573 1 Stage 2 Local 1000 2606 1.547e-015 3.299e+004 42.24 Stage 2 Search GlobalSearch stopped because it analyzed all the trial points. All 3 local solver runs converged with a positive local solver exit flag. x = 1.0e-007 * 0.0414 0.1298 fval = 1.5467e-015```

You might obtain a different result when running this problem, since the random stream was in an unknown state at the beginning of the run.

5. Restore the state of the random number stream:

`rng(stream)`
6. Run the problem again. You get the same output.

```[x,fval] = run(gs,problem) Num Pts Best Current Threshold Local Local Analyzed F-count f(x) Penalty Penalty f(x) exitflag Procedure 0 465 422.9 422.9 2 Initial Point 200 1730 1.547e-015 1.547e-015 1 Stage 1 Local ... Output deleted to save space ... x = 1.0e-007 * 0.0414 0.1298 fval = 1.5467e-015```

### Parallel Processing and Random Number Streams

You obtain reproducible results from `MultiStart` when you run the algorithm in parallel the same way as you do for serial computation. Runs are reproducible because `MultiStart` generates pseudorandom start points locally, and then distributes the start points to parallel processors. Therefore, the parallel processors do not use random numbers.

To reproduce a parallel `MultiStart` run, use the procedure described in Steps to Take in Reproducing Results. For a description of how to run `MultiStart` in parallel, see How to Use Parallel Processing in Global Optimization Toolbox.