ovidiu_dtp's avatar

How to test random

I have to implement a feature that half the time has one behavior, and half the time, another behavior, and for that I need to use random. But how could I test to see if it is actually random? That in the long run, results are not biased? It is something like:

if(somethingRandom50percentChance()) { doOneThing(); } else { doAnotherThing; }

I am a bit confused on how to deal with this if I am to try TDD. I just don't know how to start my tests doe to randomness.

0 likes
4 replies
ohffs's avatar

I'm not sure I'm 100% getting you, but you could have an array of true/false values split 50/50 - then loop over them? So something like :

$trues = [1, 1, 1, 1, 1];
$falses = [0, 0, 0, 0, 0];
$pool = array_merge($trues, $falses);
shuffle($pool);
// now you have a "random" pool of exactly 50/50 true/false
pmall's avatar

I don't think this is something that should be tested. Use php mt_rand(0, 1), ensure it get used, ensure you get whats expected when the result is 0 and when the result is 1. That's about all you can do.

ovidiu_dtp's avatar

My plan was to use the random function, because the 50-50 distribution may need to change in future. It may be based on some input. The code to make it work is trivial. But I do not know how to test it.

The idea with array shuffling is interesting. Using it instead of the normal rand would make sure I would have the distribution, but the bad thing is that I can't say that any given event has a x% chance after the first few, because once you reach the end of the array, you know exactly what you will get if you count the previous results. A way to deal with this, would be to make large arrays filled with the desired proportion of 0 and 1 and iterate over all.

ohffs's avatar

If you've watched the JS generators video you should be able to use the same thing in PHP. Just make a sort of 'pseudo generator' that takes some kind of distribution/percentage and builds it's array - then just keep taking values from it.

I don't think there's really a "proper" way to test randomness. At least not one that I've figured out or come across ;-) I've got a bit of python code just now that interacts with some nano-technology kit which has a lot of random "stuff" happening and it's a real pita to test - so I feel your pain ;-)

1 like

Please or to participate in this conversation.