---
url: /config/fakerandom.md
---

# fakeRandom  5.2.0 {#fakerandom}

* **Type:** `{ enableGlobally?: boolean }`

Options for [`vi.useFakeRandom()`](/api/vi#vi-usefakerandom).

## fakeRandom.enableGlobally

* **Type:** `boolean`
* **Default:** `false`

Should Vitest automatically call [`vi.useFakeRandom()`](/api/vi#vi-usefakerandom) before it imports the setup files and the test file. Then `Math.random`, `crypto` and `node:crypto` return values from the [`seed`](/config/seed) option in the whole file, also in libraries that keep a reference to these functions when they load, such as lodash and `uuid`.

```js [vitest.config.js]
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    fakeRandom: {
      enableGlobally: true,
    },
  },
})
```

If a test calls [`vi.useRealRandom()`](/api/vi#vi-userealrandom), Vitest calls `vi.useFakeRandom()` again before the next test.

If the config does not set a [`seed`](/config/seed), each run gets new values. For values that must be the same on every run, for example IDs in a snapshot, set `seed` in the config.

A module keeps the functions that it found when it loaded. So a module that kept the fake functions still returns seeded values after `vi.useRealRandom()`. A module that loaded before Vitest imported the setup files, for example in a custom [environment](/config/environment), still returns real values. For other values that the fake cannot repeat, see [`vi.useFakeRandom`](/api/vi#vi-usefakerandom).

This option cannot be used together with [`restoreRandom`](/config/restorerandom).

::: warning
On Node.js 22 and in browser mode, Vitest doesn't know which [concurrent test](/api/test#test-concurrent) calls the functions, so the fake functions throw an error in concurrent tests and suites. Use [`context.random`](/guide/test-context#random) in concurrent tests there, or run a concurrent test with the real functions with the [`fakeRandom: false`](/api/test#fakerandom) test option. On Node.js 24 and later, each concurrent test gets its own values.

Hooks that run at the same time (see [`sequence.hooks`](/config/sequence#sequence-hooks)) share the values of their test, so the fake functions throw an error in them on every Node.js version.
:::
