Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Test using srand (seed)

Using the srand function we can set the location of the rand pseudo-random generator. That will fix the random numbers.

Using this tactic we will probably have to run the code once with a certain seed, observe the result and then fix it as the expected result for future runs with the same seed.

The nice thing about this is that it will generate an infinite number of random values with the need for you ro prepare them.

use strict;
use warnings;

use Test::More;
use MyRandomApp qw(dice);

srand(1);
is dice(10), 1;

srand(2);
is dice(10), 10;

srand(3);
is dice(10), 8;

srand(4);
is dice(10), 7;
is dice(10), 6;
is dice(10), 1;
is dice(10), 8;

done_testing;
prove -lv t/test-with-seed.t