Skip to content

Add weighted selection  #1314

@gatear

Description

@gatear

Support weighted faking in custom providers.

Inspired from https://fakerjs.dev/api/helpers#weightedarrayelement

faker.helpers.weightedArrayElement([
{ weight: 5, value: 'sunny' }, 
{ weight: 4, value: 'rainy' },
{ weight: 1, value: 'snowy' }
]) // 'sunny', 50% of the time, 'rainy' 40% of the time, 'snowy' 10% of the time

With custom providers, we could make a YML provider with weights associated to each value

en:
  faker:
    insectsfromfile:
      ants: [5, 'Driver ant', 4, 'Fire ant', 1, 'Harvester ant']

Questions

  • Is there a way to achieve weighted faking with current codebase ?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions