Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto validate correctness of functions which use random?

There are some application domains(e.g. GameDev) in which a lot of functions should be created using random values for producing their output. One of examples is presented below:

def generate_key(monster_key_drop_coef):
    key_letters = string.ascii_uppercase
    rand = random.random()
    if monster_key_drop_coef < rand:
        return None

    button = {}
    button["type"] = random.choice([1,2,3])
    button["letter"] = random.choice(key_letters)
    return button

This function generates the item's drop based on several random operations. The problem appears if you want to validate automatically correctness of this function. Generated values are not deterministic and writing of regression tests seems to be impossible.

My questions are:

  1. Is this possible to write useful regression tests for this type of functions?
  2. Is there any general approach for creating some other type of tests in this case?
like image 864
Aliaksei Ramanau Avatar asked Jun 11 '12 16:06

Aliaksei Ramanau


1 Answers

One of useful unit-tests is presented below:

def test_generate_key():
    button_list = []
    for _ in range(1, 1000):
        button_list.append(generate_key(0.2))

    is_all_none = True
    is_not_none = False
    for key in button_list:
        is_all_none &= (key is None)
        is_not_none |= (key is not None)

    assert is_all_none == False
    assert is_not_none == True

It validates function signature, cover all lines of function's code(good probability) and will pass in 99.999% cases. Also validated that function produces some drop at least one from 1000 and sometimes doesn't generate drop. 0.2 is probability of an item's drop.

like image 68
Aliaksei Ramanau Avatar answered Sep 23 '22 19:09

Aliaksei Ramanau