mark.warren
2020-4-28 07:34:18

Does anyone have an approach for unit testing functions that return a random result? In the past I have just run the function say 10,000 times and checked that the result is one that is expected each time, but this is obviously inefficient and not conclusive.


soegaard2
2020-4-28 07:35:21

Interesting question,


soegaard2
2020-4-28 07:36:10

Usually the random number generator has a seed value.


soegaard2
2020-4-28 07:36:27

If you supply your own seed, then you get the same sequence each time.


mark.warren
2020-4-28 07:37:13

@soegaard2 That’s an interesting idea.


soegaard2
2020-4-28 07:38:02

Another option is to let the random number generator be an argument of the function (which defaults to the standard number generator).


soegaard2
2020-4-28 07:38:29

Then at test time you can supply a number generator that returns what ever number your test function wants to test.


mark.warren
2020-4-28 07:39:59

@soegaard2 Yes that does sound like a good plan.


c
2020-4-28 18:01:06

@c has joined the channel


dan.hillier.anderson
2020-4-28 20:07:50

@dan.hillier.anderson has joined the channel