• 0

How to test random dice roll function?

Given a function,

`const RandomDiceRoll = () => return num; `

which returns a random number between 1 to 6.

As a tester for this function, how would you verify that the function returns the random value each time ?

Testing truly randomness is hard. There is a lot of research has been done on this topic in academics and in industry. But for our interview purpose we can show why testing randomness is hard then walk through one possible solution.

Why is it hard :

RandomDiceRoll function might be implemented as follows :

const COUNT = 0;

const RandomDiceRoll = () => {

COUNT++;

return COUNT mod 6;

};

One Possible Solution :

Call the function many times and plot a graph

X-axis attempt number

Y-axis all possible values from 1 - 6

Based on this graph we can predict if the function is random or not.