Testing Random values

174 views
Skip to first unread message

witali mik

unread,
Nov 30, 2013, 8:03:39 AM11/30/13
to clean-code...@googlegroups.com
Hello Guys,

currently i stuck at a Problem. Iam developing a Browserbased RPG in PHP. So there ive got a shop where i can purchase items with random values based on users seed. Instead of filling my Repository with items i decidet to generate a seed and just store the seed value and regenerate the items everytime.

Now i can currently test if the generator created any items, but then the specific items may could contain invalid attribute modifiers, like a level 100 sword cannot increase just 10 strength points of a user.

I hope someone can me give a good advise how to properly test random generated values.

best regards,

sorry for my english grammar

Rusi Filipov

unread,
Dec 1, 2013, 2:31:30 PM12/1/13
to clean-code...@googlegroups.com
You could try to pass the random number generator via the constructor of the class that is using it. Then in your test, you could initialize the generator with a fixed, known seed and pass it to the production class. Since the seed forces the generator to generate a derministic sequence, using a fixed seed in the test you can predict the sequence in advance.

George Dinwiddie

unread,
Dec 1, 2013, 2:57:08 PM12/1/13
to clean-code...@googlegroups.com
Witali,

On 11/30/13 8:03 AM, witali mik wrote:
> Hello Guys,
>
> currently i stuck at a Problem. Iam developing a Browserbased RPG in
> PHP. So there ive got a shop where i can purchase items with random
> values based on users seed. Instead of filling my Repository with items
> i decidet to generate a seed and just store the seed value and
> regenerate the items everytime.
>
> Now i can currently test if the generator created any items, but then
> the specific items may could contain invalid attribute modifiers, like a
> level 100 sword cannot increase just 10 strength points of a user.

It sounds like you've got a couple problems wrapped up together.
1. the ability to generate a random sequence
2. the ability to generate items with a random sequence
3. the ability to generate a single item

The problem you mention seems to only affect #3. Can you write a test
that guarantees that your Sword object cannot have an invalid collection
of attributes?

- George

--
----------------------------------------------------------------------
* George Dinwiddie * http://blog.gdinwiddie.com
Software Development http://www.idiacomputing.com
Consultant and Coach http://www.agilemaryland.org
----------------------------------------------------------------------

witali mik

unread,
Dec 2, 2013, 4:32:14 AM12/2/13
to clean-code...@googlegroups.com
Hello George,

well its not that easy, there are several attributes on an Item, you can have either 0 or something bigger than 0 as attribute modifier. if the value is bigger than 0 i need to test it. Currently there are some assert methods in the PHPUnit lib, but there is nothing something like "assertMaybe". i could just test the values within specific range, like assertInRange($min,$max). but iam not sure if this is a good way or do i even have to test the values?

Uncle Bob

unread,
Dec 2, 2013, 5:48:18 AM12/2/13
to clean-code...@googlegroups.com
Witali,

There are two ways to test something that is random.  The first way is to replace the random number generator with a stub that generates a fixed known sequence.  Then write your tests to take advantage of that.  This works, but can be very fragile unless you do it in tiny little increments. 

The second way is to do statistical testing.  You execute the functions you are testing 100 times, and do a statistical analysis on the results.  This works too, but consumes a lot of computer power and can sometimes result in false positives, or false negatives.  

Of the two, the first way is usually better; but it forces you to design your system such that the random number generator is decoupled from the rest of the system, and that system is broken up into a suite of highly decoupled functions.  You can test that each of those functions behave properly by stubbing the random number generator.  

caio...@gmail.com

unread,
Dec 2, 2013, 6:40:34 AM12/2/13
to clean-code...@googlegroups.com
Witali,

Thinking about your problem from a game design perspective, there is a coupling between the items' levels and their attributes, although the attributes are random. So I suggest you establish attribute limit ranges for each level. This way you would have the strength of a level 100 sword vary only between 1000 and 1500, for instance. You limit the randomness range, and test if the generated values are within that range, making your tests not worry about the randomness too much.

Hope this helps,
Caio



--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

George Dinwiddie

unread,
Dec 2, 2013, 11:10:58 PM12/2/13
to clean-code...@googlegroups.com
Witali,

It's easier for me to follow if you reply inline.

On 12/2/13 4:32 AM, witali mik wrote:
> Hello George,
>
> well its not that easy, there are several attributes on an Item, you can
> have either 0 or something bigger than 0 as attribute modifier. if the
> value is bigger than 0 i need to test it.

I'm not sure I understand that.

I'm talking about the object creation. If you set a particular
attribute, then other attributes may be constrained in their range of
allowed values. Why not correct the object at that time?

- George
> --
> The only way to go fast is to go well.
> ---
> You received this message because you are subscribed to the Google
> Groups "Clean Code Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to clean-code-discu...@googlegroups.com.
> To post to this group, send email to clean-code...@googlegroups.com.
> Visit this group at http://groups.google.com/group/clean-code-discussion.

witali mik

unread,
Dec 3, 2013, 2:42:35 AM12/3/13
to clean-code...@googlegroups.com
Hello Guys,

thx for the Hints, i gues iam fine when i test the possible attribute ranges on the Items. The only thing is, that if someone change the formula for calculating the attributes, they tests may fail, but i gues this would be a specific usecase so for another usecase i have to write another test.

currently i have that cucumber feature and it feels right + easy to test

Scenario: Generate Item
   
Given I'am user "BlackScorp"
    When I generate an item "<item_type>"
    Then I should see an item "<item_type>"
    And the item should have following attributes:
    | name | min | max |
    | strength | <min_strength> | <max_strength> |
    | constitution | <min_constitution> | <max_constitution> |
    | intelligence | <min_intelligence> | <max_intelligence> |
    | willpower | <min_willpower> | <max_willpower> |
    | dexterity | <min_dexterity> | <max_dexterity> |
    And the item should cost between <min_price> and <max_price>
    And the item should contain following informations:
    | name |
    | type |
    | image |
    | description |

George Dinwiddie

unread,
Dec 3, 2013, 8:12:33 AM12/3/13
to clean-code...@googlegroups.com
Witali,

On 12/3/13 2:42 AM, witali mik wrote:
> Hello Guys,
>
> thx for the Hints, i gues iam fine when i test the possible attribute
> ranges on the Items. The only thing is, that if someone change the
> formula for calculating the attributes, they tests may fail, but i gues
> this would be a specific usecase so for another usecase i have to write
> another test.

Your tests are specifications. If you want different behavior, you
SHOULD write another test.

>
> currently i have that cucumber feature and it feels right + easy to test
>
> |
> Scenario:GenerateItem
> GivenI'am user "BlackScorp"
> When I generate an item "<item_type>"
> Then I should see an item "<item_type>"
> And the item should have following attributes:
> | name | min | max |
> | strength | <min_strength> | <max_strength> |
> | constitution | <min_constitution> | <max_constitution> |
> | intelligence | <min_intelligence> | <max_intelligence> |
> | willpower | <min_willpower> | <max_willpower> |
> | dexterity | <min_dexterity> | <max_dexterity> |
> And the item should cost between <min_price> and <max_price>
> And the item should contain following informations:
> | name |
> | type |
> | image |
> | description |
> |
>

That is a very weak test and will not tell you much unless you run it
very many times. I would suggest a unit test on the constructor of the
object that demonstrates disallowing illegal combinations of attributes.

- George
> > an email to clean-code-discu...@googlegroups.com
> <javascript:>.
> > To post to this group, send email to
> clean-code...@googlegroups.com <javascript:>.
> <http://groups.google.com/group/clean-code-discussion>.
>
> --
>
> ----------------------------------------------------------------------
> * George Dinwiddie * http://blog.gdinwiddie.com
> Software Development http://www.idiacomputing.com
> Consultant and Coach http://www.agilemaryland.org
>
> ----------------------------------------------------------------------
>
> --
> The only way to go fast is to go well.
> ---
> You received this message because you are subscribed to the Google
> Groups "Clean Code Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to clean-code-discu...@googlegroups.com.
Message has been deleted

witali mik

unread,
Dec 3, 2013, 9:24:55 AM12/3/13
to clean-code...@googlegroups.com

Hello George,

thats the point, iam going to use an example table there http://docs.behat.org/guides/1.gherkin.html#scenario-outlines
which i generate in Excel first and export it as CSV, so this test will run like many 1000 times
>     <javascript:>.
>      > To post to this group, send email to
>     clean-code...@googlegroups.com <javascript:>.
>      > Visit this group at
>     http://groups.google.com/group/clean-code-discussion
>     <http://groups.google.com/group/clean-code-discussion>.
>
>     --
>
>     ----------------------------------------------------------------------
>         * George Dinwiddie * http://blog.gdinwiddie.com
>         Software Development http://www.idiacomputing.com
>         Consultant and Coach http://www.agilemaryland.org
>
>     ----------------------------------------------------------------------
>
> --
> The only way to go fast is to go well.
> ---
> You received this message because you are subscribed to the Google
> Groups "Clean Code Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
Reply all
Reply to author
Forward
0 new messages