Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

* gb notes that fdisk thinks his cdrom can store one terabyte -- Seen on #Linux


devel / comp.programming / Programming exercise - choose_k_of_n_then_select()

SubjectAuthor
* Programming exercise - choose_k_of_n_then_select()Tim Rentsch
+* Programming exercise - choose_k_of_n_then_select()Paul N
|`- Programming exercise - choose_k_of_n_then_select()Tim Rentsch
`- Programming exercise - choose_k_of_n_then_select()Julio Di Egidio

1
Programming exercise - choose_k_of_n_then_select()

<86cz6wfz88.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=4031&group=comp.programming#4031

  copy link   Newsgroups: comp.programming
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.programming
Subject: Programming exercise - choose_k_of_n_then_select()
Date: Sun, 29 Jan 2023 20:00:39 -0800
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <86cz6wfz88.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="fd425b27726ecb76a3994e723f0bd6b3";
logging-data="3305284"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/EzSnjodS4GK5Y+XKtCgj99D6Rl2/2E2A="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:5L+fjqdIQM08Td5zpAwod9zoxgw=
sha1:+mmekLgS4j8MnhBeqvx9O3SmGvE=
 by: Tim Rentsch - Mon, 30 Jan 2023 04:00 UTC

I offer below a programming exercise, more in the spirit of fun than
being really challenging. The effort needed isn't trivial but it
shouldn't be huge either. The exercise was inspired by some recent
discussion in comp.lang.{c,c++}.

Exercise: write code to give a definition for the interface below
(the interface is written for C, but feel free to write a solution,
along with the corresponding interface, in a different language):

typedef unsigned long UL;
typedef UL RNG( void );

UL choose_k_of_n_then_select(
RNG rng, UL rng_max, UL n, UL k, UL j
);

The parameters may be assumed to obey the following constraints
(i.e., the constraints may be asserted at the start of the function
definition)

rng != 0
j <= k
k <= n
n < rng_max

Problem: rng is a random number generator function that returns
values uniformly distributed between 0 and rng_max, inclusive (so
rng_max+1 possible values. Choose k+1 distinct random values (using
the supplied function rng) in the range between 0 and n, inclusive
(so n+1 possible values). Of these k+1 distinct values, return the
j'th value in ascending order (so for j=0 return the least value,
for j=k return the largest value, etc).

It's important that the random selection be unbiased, with all of
the (n+1) choose (k+1) possible sets being equally likely (of
course under the assumption that rng is a "good" random number
generator). However it is also important that the code work
even if rng is "poor", as for example it first returns all the
even numbers and then returns all the odd numbers. It is safe
to assume that rng is not pathologically bad: it might be
really awful, but it will not be malicious.

For purposes of testing, if k is set equal to n, the result of
any j <= k should be equal to j, so

choose_k_of_n_then_select( rng, -1, 100, 100, 0 ) == 0
choose_k_of_n_then_select( rng, -1, 100, 100, 1 ) == 1
...
choose_k_of_n_then_select( rng, -1, 100, 100, 99 ) == 99
choose_k_of_n_then_select( rng, -1, 100, 100, 100 ) == 100

(with 'rng' being any suitable rng, even a poor one).

Note that rng_max might be close to n, which means it's important to
take that possibility into account in producing random numbers, so
that there is no bias.

Good solutions should not impose any artificial limitations on the
values of j, k, n, and rng_max.

I have written code to do this but will not be posting it for at
least a week. Have fun!

Re: Programming exercise - choose_k_of_n_then_select()

<79b3d47f-0363-47b8-b503-cbdece54ae6dn@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=4033&group=comp.programming#4033

  copy link   Newsgroups: comp.programming
X-Received: by 2002:a05:6214:a54:b0:537:74af:5233 with SMTP id ee20-20020a0562140a5400b0053774af5233mr982785qvb.82.1675087271741;
Mon, 30 Jan 2023 06:01:11 -0800 (PST)
X-Received: by 2002:a05:6808:4381:b0:364:ea63:e9c1 with SMTP id
dz1-20020a056808438100b00364ea63e9c1mr2129033oib.252.1675087270899; Mon, 30
Jan 2023 06:01:10 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.uzoreto.com!feeder1.cambriumusenet.nl!feed.tweak.nl!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.programming
Date: Mon, 30 Jan 2023 06:01:10 -0800 (PST)
In-Reply-To: <86cz6wfz88.fsf@linuxsc.com>
Injection-Info: google-groups.googlegroups.com; posting-host=88.111.34.149; posting-account=0B-afgoAAABP6274zLUJKa8ZpdIdhsYx
NNTP-Posting-Host: 88.111.34.149
References: <86cz6wfz88.fsf@linuxsc.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <79b3d47f-0363-47b8-b503-cbdece54ae6dn@googlegroups.com>
Subject: Re: Programming exercise - choose_k_of_n_then_select()
From: gw7rib@aol.com (Paul N)
Injection-Date: Mon, 30 Jan 2023 14:01:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Paul N - Mon, 30 Jan 2023 14:01 UTC

On Monday, January 30, 2023 at 4:00:45 AM UTC, Tim Rentsch wrote:
> I offer below a programming exercise, more in the spirit of fun than
> being really challenging. The effort needed isn't trivial but it
> shouldn't be huge either. The exercise was inspired by some recent
> discussion in comp.lang.{c,c++}.
>
> Exercise: write code to give a definition for the interface below
> (the interface is written for C, but feel free to write a solution,
> along with the corresponding interface, in a different language):
>
> typedef unsigned long UL;
> typedef UL RNG( void );
>
> UL choose_k_of_n_then_select(
> RNG rng, UL rng_max, UL n, UL k, UL j
> );
>
> The parameters may be assumed to obey the following constraints
> (i.e., the constraints may be asserted at the start of the function
> definition)
>
> rng != 0
> j <= k
> k <= n
> n < rng_max
>
> Problem: rng is a random number generator function that returns
> values uniformly distributed between 0 and rng_max, inclusive (so
> rng_max+1 possible values. Choose k+1 distinct random values (using
> the supplied function rng) in the range between 0 and n, inclusive
> (so n+1 possible values). Of these k+1 distinct values, return the
> j'th value in ascending order (so for j=0 return the least value,
> for j=k return the largest value, etc).
>
> It's important that the random selection be unbiased, with all of
> the (n+1) choose (k+1) possible sets being equally likely (of
> course under the assumption that rng is a "good" random number
> generator). However it is also important that the code work
> even if rng is "poor", as for example it first returns all the
> even numbers and then returns all the odd numbers. It is safe
> to assume that rng is not pathologically bad: it might be
> really awful, but it will not be malicious.
>
> For purposes of testing, if k is set equal to n, the result of
> any j <= k should be equal to j, so
>
> choose_k_of_n_then_select( rng, -1, 100, 100, 0 ) == 0
> choose_k_of_n_then_select( rng, -1, 100, 100, 1 ) == 1
> ...
> choose_k_of_n_then_select( rng, -1, 100, 100, 99 ) == 99
> choose_k_of_n_then_select( rng, -1, 100, 100, 100 ) == 100
>
> (with 'rng' being any suitable rng, even a poor one).
>
> Note that rng_max might be close to n, which means it's important to
> take that possibility into account in producing random numbers, so
> that there is no bias.
>
> Good solutions should not impose any artificial limitations on the
> values of j, k, n, and rng_max.
>
> I have written code to do this but will not be posting it for at
> least a week. Have fun!

Just to check, we're free to "use" rng any way we want to, as long as the results are unbiased? For example, a naïve approach might be to try again if we get a value bigger than n, but if rng_max is between 2n+2 and 3n then we could have 0 or n+1 mean 0, 1 or n+2 mean 1, etc, and only have to reject values bigger than 2n+2. Also, do we have to select numbers in the range 0 to n and reject any duplicates, or can we rig things so we are selecting randomly only from those numbers not yet selected?

Re: Programming exercise - choose_k_of_n_then_select()

<868rhkf2g2.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=4034&group=comp.programming#4034

  copy link   Newsgroups: comp.programming
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.programming
Subject: Re: Programming exercise - choose_k_of_n_then_select()
Date: Mon, 30 Jan 2023 07:48:45 -0800
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <868rhkf2g2.fsf@linuxsc.com>
References: <86cz6wfz88.fsf@linuxsc.com> <79b3d47f-0363-47b8-b503-cbdece54ae6dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="fd425b27726ecb76a3994e723f0bd6b3";
logging-data="3529229"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nR3JpfOjzXVpdkt8+5HmXCHRKYFnm+h4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:ubAjC6rMaY1CacxSIVZaWCMvLaE=
sha1:+v82lPB/g0IY+JqRABJ9Wbb+m4Q=
 by: Tim Rentsch - Mon, 30 Jan 2023 15:48 UTC

Paul N <gw7rib@aol.com> writes:

> On Monday, January 30, 2023 at 4:00:45 AM UTC, Tim Rentsch wrote:
>
>> [choosing some distinct values using 'rng' for random numbers]
>
> Just to check, we're free to "use" rng any way we want to, as long as
> the results are unbiased? For example, a naive approach might
> be to try again if we get a value bigger than n, but if rng_max is
> between 2n+2 and 3n then we could have 0 or n+1 mean 0, 1 or n+2 mean
> 1, etc, and only have to reject values bigger than 2n+2.

Right. In general if we want to get an unbiased uniform value in
some range, some results from rng() will have to be passed over
in cases where the number of possible values from calling rng()
is not an exact multiple of the number of values in the range
(which is n+1 in your example). It's necessary to do something
along these general lines, as otherwise the results will be
biased in one direction or another.

> Also, do we
> have to select numbers in the range 0 to n and reject any duplicates,
> or can we rig things so we are selecting randomly only from those
> numbers not yet selected?

Either approach is valid as far as getting the right answer is
concerned. You might prefer one of these methods over the other,
or perhaps yet a different method, considering some other aspect
of the problem, such as run-time performance or how much memory
is needed.

Re: Programming exercise - choose_k_of_n_then_select()

<a815fddb-13e7-48ff-93f7-c86ecc13b2a9n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=4036&group=comp.programming#4036

  copy link   Newsgroups: comp.programming
X-Received: by 2002:ac8:5b87:0:b0:3b8:6cb0:8d26 with SMTP id a7-20020ac85b87000000b003b86cb08d26mr433431qta.375.1675174452693;
Tue, 31 Jan 2023 06:14:12 -0800 (PST)
X-Received: by 2002:a05:6870:3308:b0:163:30ae:9323 with SMTP id
x8-20020a056870330800b0016330ae9323mr2520397oae.120.1675174452299; Tue, 31
Jan 2023 06:14:12 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.programming
Date: Tue, 31 Jan 2023 06:14:12 -0800 (PST)
In-Reply-To: <86cz6wfz88.fsf@linuxsc.com>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.99.99; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.99.99
References: <86cz6wfz88.fsf@linuxsc.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a815fddb-13e7-48ff-93f7-c86ecc13b2a9n@googlegroups.com>
Subject: Re: Programming exercise - choose_k_of_n_then_select()
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Tue, 31 Jan 2023 14:14:12 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2311
 by: Julio Di Egidio - Tue, 31 Jan 2023 14:14 UTC

On Monday, 30 January 2023 at 05:00:45 UTC+1, Tim Rentsch wrote:
<snip>
> Problem: rng is a random number generator function that returns
> values uniformly distributed between 0 and rng_max, inclusive (so
> rng_max+1 possible values. Choose k+1 distinct random values (using
> the supplied function rng) in the range between 0 and n, inclusive
> (so n+1 possible values).

That rng_max is really an error since it introduces a double step
that does not exist in reality (if you can instantiate an rng for the
range [0,rng_max], then you can as well directly instantiate one
for the range [0,n]). The requirement indeed boils down to
generating k+1 random numbers in the range [0,n]. If your intent,
as I guess, was to have one explicitly code the transformation of
range, you should have asked for an rng that (as usual) returns
numbers in [0,1[.

> Of these k+1 distinct values, return the
> j'th value in ascending order (so for j=0 return the least value,
> for j=k return the largest value, etc).

I don't think better can be done than:
1. loop to generate the random numbers
1.*. insert sorted into a containing array (ascending)
2. return the j-th element of the array.

Julio

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor