mercredi 24 avril 2019

How to pick a random value out of an array with a specific proportion?

I'm using Javascript and I would like to pick a random value out of an array. The problem is that actually I want to do the selection with specific proportions.

What does it mean?

I have the following values - 'INFO', 'CRITICAL', 'DEBUG', 'ERROR'

And I want to pick the values with the following ratio:

1. INFO - 30% 2. CRITICAL - 5% 3. DEBUG - 55% 4. ERROR - 10%

This is how I currently randomally pick a value -

var diffSev = ['INFO', 'CRITICAL', 'DEBUG', 'ERROR']
output['testSev'] = diffSev[Math.floor(Math.random() * diffSev.length)];

I would like to add a logic that will pick the values with the ratio that I need. Thanks!!




Aucun commentaire:

Enregistrer un commentaire