Possible Duplicate:
How should I test randomness?

One of the features to be developed in our application was allowing user to click a button which will choose an element at random out of (normally 5-10) values registered in the database. The randomness doesn't need to be secure or scientifically uniform, but we do need it to be somehow unpredictable and not noticeably biased to one of the value.

Any suggestion how to write a good unit test and functional test for this feature?

  • Are you testing the feature or your randomness? – Telastyn Aug 18 '12 at 2:58
  • The feature. But as I said, one of the acceptance criteria that the generated value is somehow fair and unpredictable – Louis Rhys Aug 18 '12 at 3:14
  • 1
    If the randomness doesn't need to be extremely good, then any off-the-shelf or built-in RNG should work. Set it and forget it. If you need to unit test predictably, give it the same seed value so you get the same stream of numbers out. Other than that, you need to give us more information. – Eric Aug 18 '12 at 3:27

You can use the same seed on 2 parallell processes, and if they start in the same general (in GHz processor terms) timeframe they will have the same exact seed to the static seed. Or most languages if seeded with the same static seed will have the same numerical generation (Java's a good RNG example of this API style).

To combat the inherent static predictability of this, RNG's are usually seeded with the internal system time down to micro or nano second range, giving high unpredictability in the long run and near impossible ability to predict based on time in the short, since it's internal crystal clock cycles that feed up to the API.

As an aside: all numbers generated by machines will be pseudo-random, but close enough for government work.

To get a numerical range you want just take the integer modulo (integer_range_max) of the RNG value [0-1). So RNG * Modulo % Modulo will give you the range.

Not the answer you're looking for? Browse other questions tagged or ask your own question.