Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for Random Value - Thoughts on this Approach?

OK, I have been working on a random image selector and queue system (so you don't see the same images too often).

All was going swimmingly (as far as my crappy code does) until I got to the random bit. I wanted to test it, but how do you test for it? There is no Debug.Assert(i.IsRandom) (sadly) :D

So, I got my brain on it after watering it with some tea and came up with the following, I was just wondering if I could have your thoughts?

  • Basically I knew the random bit was the problem, so I ripped that out to a delegate (which would then be passed to the objects constructor).
  • I then created a class that pretty much performs the same logic as the live code, but remembers the value selected in a private variable.
  • I then threw that delegate to the live class and tested against that:

i.e.

Debug.Assert(myObj.RndVal == RndIntTester.ValuePassed);

But I couldn't help but think, was I wasting my time? I ran that through lots of iterations to see if it fell over at any time etc.

Do you think I was wasting my time with this? Or could I have got away with:

Awesome Random Number Generator

GateKiller's answer reminded me of this:

Dilbert Random

Update to Clarify

  • I should add that I basically never want to see the same result more than X number of times from a pool of Y size.
  • The addition of the test container basically allowed me to see if any of the previously selected images were "randomly" selected.
  • I guess technically the thing here being tested in not the RNG (since I never wrote that code) but the fact that am I expecting random results from a limited pool, and I want to track them.
like image 253
Rob Cooper Avatar asked Sep 23 '08 18:09

Rob Cooper


People also ask

What is random value testing?

Random testing is a black-box software testing technique where programs are tested by generating random, independent inputs. Results of the output are compared against software specifications to verify that the test output is pass or fail.

How do you check for randomness?

Hypothesis: To test the run test of randomness, first set up the null and alternative hypothesis. In run test of randomness, null hypothesis assumes that the distributions of the two continuous populations are the same. The alternative hypothesis will be the opposite of the null hypothesis.

How do you know if a set of numbers is random?

If you were to count the number of 1's and 0's in the strings you would find they both have the same number of each and so tells you little. However, for any random process any sequence within it (e.g. “11” or “101”) should have equal probability compared to another sequence of the same length.

How do computers pick random numbers?

There are two main methods that a computer generates a random number: true random number generators (TRNGs) and pseudo-random number generators (PRNGs). The former uses some phenomenon outside the computer for its number generation, whereas the latter relies on pre-set algorithms to emulate randomness².


2 Answers

Test from the requirement : "so you don't see the same images too often"

Ask for 100 images. Did you see an image too often?

like image 90
Amy B Avatar answered Sep 21 '22 13:09

Amy B


There is a handy list of statistical randomness tests and related research on Wikipedia. Note that you won't know for certain that a source is truly random with most of these, you'll just have ruled out some ways in which it may be easily predictable.

like image 28
moonshadow Avatar answered Sep 20 '22 13:09

moonshadow