vendredi 14 octobre 2022

How to generate derivative list with frequencies

I have some C++ code that picks a random item from a list. I need it to weight that randomness so that an item at place "n" has a chance equal to x/n where "x" is the chance that item one in the list will be selected. My current code is like this:

srand(time(NULL));
string a[≈9000] = {"String#1", "String#2", . . ., "String #≈9000"};
int value = rand() % ≈9000;
cout << a[value]

Note that the number notated as "≈9000" is a precise integer obscured for confidentiality. Variable names may be changed.

How can I weight it? I've come up with an equivalent formula

List B[≈9000] = "Item 'n' of 'a' times ≈9000 ÷ n"

Though you might notice that that isn't accurate CPP notation. Do y'all have any ideas how I can implement this?




Aucun commentaire:

Enregistrer un commentaire