Skip to content

随机器

¥Randomizer

Randomizer 接口允许你在 Faker 中使用自定义随机源。

¥The Randomizer interface allows you to use a custom randomness source within Faker.

重要

Faker 的默认 Randomizer 在大多数情况下就足够了。仅当你想使用它来实现特定目标时才更改此设置,例如与其他实例/工具共享相同的随机生成器。

¥Faker's default Randomizer is sufficient in most cases. Change this only if you want to use it to achieve a specific goal, such as sharing the same random generator with other instances/tools.

我们考虑了两种可能需要这样做的相关用例:

¥There are two connected use cases we have considered where this might be needed:

  1. 在多个 Faker 实例中重复使用相同的 Randomizer

    ¥Re-Use of the same Randomizer within multiple Faker instances.

  2. 使用来自第三方的随机数生成器库。

    ¥The use of a random number generator from a third party library.

内置 Randomizer

¥Built-In Randomizers

Faker 附带两种变体

¥Faker ships with two variations

ts
import {
  generateMersenne32Randomizer, // Default prior to v9
  generateMersenne53Randomizer, // Default since v9
} from '@faker-js/faker';

const randomizer = generateMersenne53Randomizer();

32 位 Randomizer 速度更快,但 53 位 Randomizer 生成更好的随机值(重复次数明显减少)。

¥The 32bit Randomizer is faster, but the 53bit Randomizer generates better random values (with significantly fewer duplicates).

但你也可以通过实现 相关接口 来实现自己的逻辑。

¥But you can also implement your own by implementing the related interface.

使用 Randomizer

¥Using Randomizers

Randomizer 有在实例构造期间设置:

¥A Randomizer has to be set during construction of the instance:

ts
import { Faker, Randomizer } from '@faker-js/faker';

const customFaker = new Faker({
  locale: ...,
  randomizer: ...,
});

以下方法以 Randomizer 作为参数:

¥The following methods take a Randomizer as argument:

重新使用 Randomizer

¥Re-Using a Randomizer

有时可能需要在两个不同的语言环境中生成值。例如中国人可能拥有英语身份,以简化与外国人的交流。虽然这也可以通过两个独立的 Faker 实例实现,如下所示:

¥Sometimes it might be required to generate values in two different locales. E.g. a Chinese person might have an English identity to simplify the communication with foreigners. While this could also be achieved with two independent Faker instances like this:

ts
import { fakerEN, fakerZH_TW } from '@faker-js/faker';

fakerZH_TW.seed(5);
fakerEN.seed(5);

const firstName = fakerZH_TW.person.firstName(); // 炫明
const alias = fakerEN.person.firstName(); // Arthur

当只播种其中一个时,可能会出现可重复性问题。

¥There might be issues regarding reproducibility, when seeding only one of them.

通过在两个实例之间共享 Randomizer,你可以通过同时影响所有实例来忽略此问题。

¥By sharing a Randomizer between the two instances, you omit this issue by affecting all instances simultaneously.

注释

如果播种发生在与数据生成不同的位置(例如由于嵌套),这一点会变得更加重要。

¥This gets more important if the seeding happens at a different location than the data generation (e.g. due to nesting).

ts
import { en, Faker, Randomizer, zh_TW } from '@faker-js/faker';

const randomizer: Randomizer = ...;

const customFakerEN = new Faker({
  locale: en,
  randomizer,
});

const customFakerZH_TW = new Faker({
  locale: [zh_TW, en],
  randomizer,
});

randomizer.seed(5);
// customFakerEN.seed(5); // Redundant
// customFakerZH_TW.seed(5); // Redundant

const firstName = fakerZH_TW.person.firstName(); // 炫明
const alias = fakerEN.person.firstName(); // John (different from before, because it is now the second call)

当尝试在第三方库中使用 faker 的随机数生成器时,这也很重要。例如一些可以从 RegExp 生成 string 的库也可以使用自定义随机数生成器进行定制,并且由于它们将在相同的上下文中使用,因此依赖相同的随机源来确保值是可重现的。

¥This is also relevant when trying to use faker's random number generator in third party libraries. E.g. some libraries that can generate strings from a RegExp can be customized with a custom random number generator as well, and since they will be used in the same context it makes sense to rely on the same randomness source to ensure the values are reproducible.

第三方 Randomizer

¥Third-Party Randomizers

有时你可能想要使用自定义/第三方随机数生成器。这可以通过实现你自己的 Randomizer 并将其传递给 支持的方法 来实现。

¥Sometimes you might want to use a custom/third-party random number generator. This can be achieved by implementing your own Randomizer and passing it to supported methods.

注释

Faker 不为第三方库提供 Randomizers,也不提供弥合库之间差距的支持。以下示例展示了如何实现接口,但未测试它们的正确性。欢迎为其他流行软件包提交更多 Randomizer 示例。

¥Faker does not ship Randomizers for third-party libraries and does not provide support for bridging the gap between libraries. The following examples show how the interface can be implemented, but they are not tested for correctness. Feel free to submit more Randomizer examples for other popular packages.

纯随机

¥Pure-Rand

以下是基于 pure-randRandomizer 的示例:

¥The following is an example for a pure-rand based Randomizer:

ts
import { Faker, Randomizer, SimpleFaker } from '@faker-js/faker';
import { RandomGenerator, xoroshiro128plus } from 'pure-rand';

export function generatePureRandRandomizer(
  seed: number | number[] = Date.now() ^ (Math.random() * 0x100000000),
  factory: (seed: number) => RandomGenerator = xoroshiro128plus
): Randomizer {
  const self = {
    next: () => (self.generator.unsafeNext() >>> 0) / 0x100000000,
    seed: (seed: number | number[]) => {
      self.generator = factory(typeof seed === 'number' ? seed : seed[0]);
    },
  } as Randomizer & { generator: RandomGenerator };
  self.seed(seed);
  return self;
}

Faker v9.7 中文网 - 粤ICP备13048890号.