Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #147865
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
| Newsgroups | comp.lang.c |
| Subject | Re: Bart, a tcc "issue" |
| Date | Sun, 10 Nov 2019 03:04:57 +0000 |
| Organization | A noiseless patient Spider |
| Lines | 47 |
| Message-ID | <87mud42zza.fsf@bsb.me.uk> (permalink) |
| References | <uRIwF.17878$kE3.891@fx36.iad> <bYoxF.132400$Ko9.64189@fx48.iad> <86k189ejbd.fsf@linuxsc.com> <87sgmw3ajx.fsf@bsb.me.uk> <tIJxF.44123$hh5.23905@fx07.iad> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | reader02.eternal-september.org; posting-host="a6f2659544a53531bdb2ad125aac83f7"; logging-data="21625"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GqJNoxQ6YNNMoqwCJMS4Twx/yOFdQPPg=" |
| Cancel-Lock | sha1:pkcplGfjNfvXfll3baK4+OkIQVc= sha1:5YTg93iGFC08IDOlerFtwQKSP1w= |
| X-BSB-Auth | 1.1e4803b45154c7bbd8de.20191110030457GMT.87mud42zza.fsf@bsb.me.uk |
| Xref | csiph.com comp.lang.c:147865 |
Show key headers only | View raw
Richard Damon <Richard@Damon-Family.org> writes:
> On 11/9/19 6:16 PM, Ben Bacarisse wrote:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>> <cut>
>>> It's interesting -- three different people posted corrections or
>>> improvements to the uniform interval code, and all of the revisions
>>> have been a little bit off in one way or another. Care is needed to
>>> deal with edge cases correctly.
>>>
>>> int
>>> uniform_less_than( unsigned k ){
>>> unsigned d, r;
>>>
>>> if( k-1 > RAND_MAX || k == 0 ) oops_someone_messed_up();
>>>
>>> d = (RAND_MAX - (k-1)) / k + 1;
>>> do r = rand()/d; while( r >= k );
>>>
>>> return r;
>>> }
>>>
>>> int
>>> uniform_on_closed_interval( int a, int b ){
>>> if( a > b ) return uniform_on_closed_interval( b, a );
>>>
>>> return a + uniform_less_than( b + 1u - a );
>>> }
>>>
>>> A good test for a uniform closed-interval RNG is to check that
>>> it handles the interval 0..RAND_MAX, including both endpoints,
>>> correctly.
>>
>> Are you considering edge cases in the C standard? If so, one of those
>> has RAND_MAX == UINT_MAX.
>
> Since rand() returns an int, the only way for RAND_MAX to be UINT_MAX is
> if UINT_MAX == INT_MAX (which generally means a Signed-Magnitude machine
> or some other odd architecture). And yes, if you want to deal with that
> sort of machine a lot of care needs to be taken, as a lot of things we
> often take for granted aren't true, like that an unsigned can count the
> number of values between any two signed numbers!!
Yes, it's a gruesome corner case for some types of code.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Bart, a tcc "issue" DFS <nospam@dfs.com> - 2019-11-06 18:40 -0500
Re: Bart, a tcc "issue" Bart <bc@freeuk.com> - 2019-11-07 00:03 +0000
Re: Bart, a tcc "issue" Bart <bc@freeuk.com> - 2019-11-07 00:06 +0000
Re: Bart, a tcc "issue" Bart <bc@freeuk.com> - 2019-11-07 01:44 +0000
Re: Bart, a tcc "issue" James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-07 10:05 -0500
Re: Bart, a tcc "issue" DFS <nospam@dfs.com> - 2019-11-07 13:22 -0500
Re: Bart, a tcc "issue" DFS <nospam@dfs.com> - 2019-11-08 09:19 -0500
Re: Bart, a tcc "issue" James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-08 11:03 -0500
Re: Bart, a tcc "issue" Manfred <noname@add.invalid> - 2019-11-07 18:22 +0100
Re: Bart, a tcc "issue" Manfred <noname@add.invalid> - 2019-11-07 18:30 +0100
Re: Bart, a tcc "issue" Richard Damon <Richard@Damon-Family.org> - 2019-11-08 20:51 -0500
Re: Bart, a tcc "issue" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-08 20:59 -0800
Re: Bart, a tcc "issue" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-09 23:16 +0000
Re: Bart, a tcc "issue" Richard Damon <Richard@Damon-Family.org> - 2019-11-09 20:27 -0500
Re: Bart, a tcc "issue" Bart <bc@freeuk.com> - 2019-11-10 01:38 +0000
Re: Bart, a tcc "issue" Richard Damon <Richard@Damon-Family.org> - 2019-11-09 20:59 -0500
Re: Bart, a tcc "issue" Keith Thompson <kst-u@mib.org> - 2019-11-09 19:22 -0800
Re: Bart, a tcc "issue" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-10 03:04 +0000
Re: Bart, a tcc "issue" Richard Damon <Richard@Damon-Family.org> - 2019-11-09 23:04 -0500
Re: Bart, a tcc "issue" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-10 13:15 +0000
Re: Bart, a tcc "issue" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-10 06:04 -0800
Re: Bart, a tcc "issue" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-10 07:49 -0800
Re: Bart, a tcc "issue" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-10 17:49 +0000
Re: Bart, a tcc "issue" "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-11-11 02:44 -0800
Re: Bart, a tcc "issue" James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-11 13:57 -0500
Re: Bart, a tcc "issue" "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-11-14 16:51 -0800
Re: Bart, a tcc "issue" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 16:03 -0800
csiph-web