NetLogo Help: with-local-randomness

### with-local-randomness4.0

#### with-local-randomness [ commands ]

The commands are run without affecting subsequent random events. This is useful for performing extra operations (such as output) without changing the outcome of a model.

Example:

    ;; Run #1:
    random-seed 50 setup repeat 10 [ go ]
    ;; Run #2:
    random-seed 50 setup
    with-local-randomness [ watch one-of turtles ]
    repeat 10 [ go ]

Since `one-of` is used inside `with-local-randomness`, both runs will be identical.

Specifically how it works is, the state of the random number generator is remembered before the commands run, then restored afterwards. (If you want to run the commands with a fresh random state instead of the same random state that will be restored later, you can begin the commands with `random-seed new-seed`.)

The following example demonstrates that the random number generator state is the same both before the commands run and afterwards.

    random-seed 10
    with-local-randomness [ print n-values 10 [random 10] ]
    ;; prints [8 9 8 4 2 4 5 4 7 9]
    print n-values 10 [random 10]
    ;; prints [8 9 8 4 2 4 5 4 7 9]

Take me to the full NetLogo Dictionary