Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Matthew Flynn, World Engineer, Thomas Owens Aug 18 '12 at 7:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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
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
add comment

1 Answer

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.

share|improve this answer
add comment

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