Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Compute Unique Numbers in a Set Date: Tue, 27 Dec 2022 19:42:46 -0800 Organization: A noiseless patient Spider Lines: 26 Message-ID: <86sfh0urbd.fsf@linuxsc.com> References: <874jtgn7ls.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="38ac7d2bf94e0515b2526fa483f331a9"; logging-data="4075844"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SG5qW4nZPUm7C0NqWwaP1xGXYerQbfX8=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:nK6sUwQp0c1cNO40SUlK0LjDhoA= sha1:ILZ6qgwwtzOfjIMnldrIUq+YpKg= Xref: csiph.com comp.lang.c:168678 Ben Bacarisse writes: > Albert writes: > >> Is this the best way to generate unique random numbers in a set of 6 >> numbers? > > No, but it's a valiant attempt! > > It has a few issues. First, the number of chosen numbers (6) is > hard-wired into the function as a repeated code pattern. You want > to avoid both repeated code and code the represents something that > is, essentially, data. > > You want to aim for a function that takes two numbers, the upper > bound of the numbers that can be chosen and the number of number to > be chosen. Personally, I'd also pass a pointer to where the chosen > numbers should be written. > > But the biggest problem is the algorithm. Unless the range of > possible choices is vast (and in your case it is only 60) [...] If you look in the original posting, I think you will see that the set of values is between 1 and 59 inclusive, that is, only 59 values. (That may have been a mistake by OP, but I offer no opinion on that question.)