Skip to content

Fix: Ensure deterministic locale selection in multi-locale mode#2287

Open
just6660 wants to merge 1 commit intojoke2k:masterfrom
just6660:fix/master/issue-2283-deterministic-locales
Open

Fix: Ensure deterministic locale selection in multi-locale mode#2287
just6660 wants to merge 1 commit intojoke2k:masterfrom
just6660:fix/master/issue-2283-deterministic-locales

Conversation

@just6660
Copy link

@just6660 just6660 commented Dec 1, 2025

What does this change

This PR fixes a bug where Faker instances initialized with multiple locales would produce non-deterministic output even when seeded. It ensures that the selection of which locale to use (e.g., picking between en_US or ru_RU) respects the instance seed.

What was wrong

When Faker is acting as a proxy for multiple locales, it needs to randomly select a factory for each call. Previously, the methods _select_factory_distribution and _select_factory_choice used the global random module (via choices_distribution or random.choice).

Because seed_instance() only seeds the internal factory generators and not the global random module, the selection of the locale remained random and unseeded. This caused different outputs on subsequent runs despite using the same seed.

How this fixes it

I modified faker/proxy.py to use self._factories[0].random instead of the global random module for locale selection.

Since seed_instance() iterates through self._factories and seeds every single one of them with the same value, accessing the random generator of the first factory (_factories[0]) provides a safe, seeded, and deterministic source of randomness. This fixes the determinism issue without needing to expose a random property on the Proxy class (which is explicitly forbidden by the current architecture).

Fixes #2283

Checklist

  • [ X] I have read the documentation about CONTRIBUTING
  • [ X] I have read the documentation about Coding style
  • [ X] I have run make lint

Copy link
Collaborator

@fcurella fcurella left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some unit test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Re-seeding instance while using multiple locales doesn't help to get stable values in return

2 participants