0

There are strings and each string has a weight in a data structure. The getRandom() method should return a string randomly with its weight/total weight probability.

There is already a question on how to define this getRandom() method on stackexchange.

But this question is about how to test this getRandom() method?

Any ideas would be really helpful. Thank you.

rocky
  • 21

1 Answers1

-2

I would do it like this:

Call getRandom() millions of times. The more you call it, the more the frequency histogram of the string occurences is closer to the string weights. Then, basically, if all frequencies are close enough to the weights (using an arbitrary delta), the test passes, otherwise it fails.

Note that false positive/negative are possible, but with sensible values of N and delta it should be rare.

It is probably also possible to compute a % of likelihood more accurately using statistical methods, although it might be overhead .

Although it is not perfect in every way, it makes it possible to detect most implementation failures (resulting in a wrong distribution).

dagnelies
  • 5,493