joke2k / faker

Faker is a Python package that generates fake data for you.
https://faker.readthedocs.io
MIT License
17.34k stars 1.89k forks source link

related values don't line up, even if seed is reset #2063

Open cornfeedhobo opened 2 weeks ago

cornfeedhobo commented 2 weeks ago

When pulling things like country(), it would be useful if the country_code() matched, but it currently doesn't.

Expected behavior

Faker.seed(0)
Faker().country() # "Tanzania"
Faker.seed(0)
Faker().country_code() # "TZ"

Actual behavior

Faker.seed(0)
Faker().country() # "Tanzania"
Faker.seed(0)
Faker().country_code() # "MV"
kevindaglish commented 1 week ago

Hello cornfeedhobo,

I was able to replicate the issue have above. Looking at how the code generates the values using a random generator method which is referencing the same country data. How ever I would question if using a random generator twice with the same seed would be the right approach. As once you have a known expected value (your generated country), it would make sense to reference the country code from the same country data it has been randomised from. Below is a snippet of code which does this.

from faker.proxy import Faker as f from faker.providers.date_time import Provider from faker.generator import Generator from faker.providers import BaseProvider g = Generator() b = BaseProvider(g) p = Provider(b)

f.seed(0) c = f().country() cc = [Country for Country in p.countries if Country.name == c] for each in cc: country_code = each.alpha_2_code

I hope this is useful for you.