Peter Morris wrote:
> I was asked to write a small program that chose 6 random non-repeating
> numbers between 1..48 (lottery example). I found it very difficult. Not
> because the logic was hard, but because they gave me a pen and paper and I
> hadn't held a pen in about 5 years :-)
>
>
Reminds me of the old C++ exams in school. Everything had to be written down
to the last semicolon, and points would get deducted for the tiniest
syntactical slip-up. Unsurprisingly, scores were generally not high;
students were spoiled rotten by such things as full-screen editors and
compilers you could invoke with a single keypress.
Incidentally, your question is a two-liner in C# 3.0:
Random r = new Random();
var numbers = Enumerable.Range(1,48).OrderBy(i => r.Next()).Take(6);
It would be a one-liner in a proper functional programming language. :-)
Of course, the follow-up question might be to do the same trick for numbers
in between 1 and, say, int.MaxValue. This solution doesn't fare very well
for that case.
--
J.
http://symbolsprose.blogspot.com