derangement: code review request


M

Mark McIntyre

I've got to figure something out for future posts. What I see and what I
post are radically different.

Your news software is converting tabs into spaces. Don't use tabs in posts
to usenet.
 
Ad

Advertisements

T

Tim Rentsch

Keith Thompson said:
That's mostly good advice, but it shouldn't be applied universally.
Some abbreviations are so obvious that it doesn't make sense to expand
them, such as "IO" rather than "input_output". (And if I were writing
code that handled PCMCIA devices, I wouldn't even know how to expand
the abbreviation; all I can remember is "People Can't Memorize
Computer Industry Acronyms".)

The set of abbreviations that are sufficiently obvious depends
strongly on the domain and the expected audience. In my work, "GPT"
and "GSI" are obvious; most people who don't know what they stand for
are unlikely to want to read my code anyway.

Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations", by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.
Lots of acronyms are "words" for most practical purposes, and of
course some have even made their way into standard dictionaries -
sonar, radar, and now even DSL (if I'm not mistaken). Standard
acronyms often are acceptable as "words", abbreviations almost always
not. In any case the rules for how to treat abbreviations and how
to treat acronyms should treat the two cases distinctly.

Unfortunately, most people tend to err in the direction of
abbreviating too much.

HTH, HAND, YMMV.

Humorous footnote - I've seen the acronym HTH at the end of countless
articles by now, but I still don't know what it means. :)

(And yes, I'm sure I could find a meaning online if I needed to, but
somehow it just hasn't seemed important enough yet...)
 
M

Merrill & Michele

[...] >Time Rentsch:
A couple of the names use abbreviations as part of the name (eg,
'fam_size'). I recommend using the whole word, and always avoiding
abbreviations. It takes a certain amount of intestinal fortitude to
do this, as the temptation to make exceptions is strong (but don't
give in!). The more I follow this practice the more I see the
benefits. For function local variables, it's common to allow single
letters to be used as "words"; these aren't abbreviations in the
sense meant here.
That's mostly good advice, but it shouldn't be applied universally.
Some abbreviations are so obvious that it doesn't make sense to expand
them, such as "IO" rather than "input_output". (And if I were writing
code that handled PCMCIA devices, I wouldn't even know how to expand
the abbreviation; all I can remember is "People Can't Memorize
Computer Industry Acronyms".)

The set of abbreviations that are sufficiently obvious depends
strongly on the domain and the expected audience. In my work, "GPT"
and "GSI" are obvious; most people who don't know what they stand for
are unlikely to want to read my code anyway.
"Tim Rentsch wrote:
Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations", by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.
Lots of acronyms are "words" for most practical purposes, and of
course some have even made their way into standard dictionaries -
sonar, radar, and now even DSL (if I'm not mistaken). Standard
acronyms often are acceptable as "words", abbreviations almost always
not. In any case the rules for how to treat abbreviations and how
to treat acronyms should treat the two cases distinctly.



Humorous footnote - I've seen the acronym HTH at the end of countless
articles by now, but I still don't know what it means. :)

(And yes, I'm sure I could find a meaning online if I needed to, but
somehow it just hasn't seemed important enough yet...)

I have been trying for a month now to figure out what OP means, and for the
sake of language clarity, I would like to know whether it is the Original
Post or the Original Poster. The former is an 'it' while the latter has the
attribute of sex (and therefore gender). If I were not substituting a
question for an answer, I would say "Happy To Help." MPJ
 
E

Edmund Bacon

Merrill & Michele said:
I have been trying for a month now to figure out what OP means, and for the
sake of language clarity, I would like to know whether it is the Original
Post or the Original Poster. The former is an 'it' while the latter has the
attribute of sex (and therefore gender). If I were not substituting a
question for an answer, I would say "Happy To Help." MPJ

OP Original Poster
HTH Hope this helps
HAND Have a nice day

For more info see
http://www.utdallas.edu/ir/tcs/techsupp/acronyms.htm
 
K

Keith Thompson

%CC-I-ANACHRONISM, The "=-" operator is an obsolete form,
and may not be portable.
[...]
I guess many other people have been bitten by that and bitterly complained
until DEC introduced this *badly* needed warning in VAX C, otherwise the
nicest pre-ANSI C compiler I have ever used.

Probably so.

Strictly speaking it's an informational message ("-I-"), which is
weaker than a warning ("-W-"), which I think makes it easier to
suppress, but that shouldn't matter unless you're foolish enough to
turn off diagnostics.

The best solution for anyone still programming on VMS^H^H^H OpenVMS is
probably to use the newer DECC rather than VAXC.
 
Ad

Advertisements

K

Keith Thompson

In <[email protected]> "Merrill & Michele"
In real world implementations, it is the value of RAND_MAX that is chosen
according to the properties of type int (among other criteria), and not
the other way round.

Agreed, with emphasis on "among other criteria". (Solaris has 32-bit
ints, but RAND_MAX==32767.)
The standard requires both INT_MIN and RAND_MAX to be at least 32767.
However, the type of RAND_MAX need not be int, only its value must be
in the range of the type int (it is defined as an integer constant
expression).

I suppose an implementation could define RAND_MAX with a type other
than int, but I can't think of any reason to do so other than sheer
perversity. I would expect it to be an integer-constant or some
simple expression, and it has to be within the range of type int.
 
K

Keith Thompson

Tim Rentsch said:
Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations", by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.

There are two competing definitions for the word "acronym". One
(which I prefer and consider to be more correct) says that an acronym
is an abbreviation formed from the initial letter or letters of
several words *that is pronounced as a word*. The other doesn't
require that it be pronounced as a word. (I suppose "IO" is an
ambiguous case.)

None of which affects the main point, of course.
 
M

Merrill & Michele

"Merrill & Michele"
In real world implementations, it is the value of RAND_MAX that is chosen
according to the properties of type int (among other criteria), and not
the other way round.
"Keith Thompson"
Agreed, with emphasis on "among other criteria". (Solaris has 32-bit
ints, but RAND_MAX==32767.)


I suppose an implementation could define RAND_MAX with a type other
than int, but I can't think of any reason to do so other than sheer
perversity. I would expect it to be an integer-constant or some
simple expression, and it has to be within the range of type int.

It's generally not wise to snip the question and then beg it. MPJ
 
T

Tim Rentsch

Keith Thompson said:
There are two competing definitions for the word "acronym". One
(which I prefer and consider to be more correct) says that an acronym
is an abbreviation formed from the initial letter or letters of
several words *that is pronounced as a word*. The other doesn't
require that it be pronounced as a word. (I suppose "IO" is an
ambiguous case.)

Right. Historically I think the "pronounceable" definition came
first. These days I think more people are used to acronym meaning any
string of initial letters that is used as a word (i.e., without regard
to whether the word is pronounceable or not); for example, I think
most people would say TLA is itself a three letter acronym. Acronyms
that are pronounced/used as regular words eventually become just
regular words and we tend to forget their acroynm-ness. "Laser" is an
example of that, or "spool" (and in fact I used the word spool for
quite a while before finding out that it is an acronym). So being an
acronym in that earlier sense eventually becomes more of an
etymological footnote than anything else.

None of which affects the main point, of course.

Roger that.
 
K

Keith Thompson

Merrill & Michele said:
"Merrill & Michele"
[...]
In real world implementations, it is the value of RAND_MAX that is chosen
according to the properties of type int (among other criteria), and not
the other way round.
"Keith Thompson"
Agreed, with emphasis on "among other criteria". (Solaris has 32-bit
ints, but RAND_MAX==32767.)


I suppose an implementation could define RAND_MAX with a type other
than int, but I can't think of any reason to do so other than sheer
perversity. I would expect it to be an integer-constant or some
simple expression, and it has to be within the range of type int.

It's generally not wise to snip the question and then beg it. MPJ

Um, what question did I beg?

I wasn't actually trying to answer the original question; I was
elaborating on a couple of points that Dan Pop had made.

If the question you're referring to is:

] Are you ever going to get in trouble with r declared as an int? Can
] RAND_MAX be 10^9 without forcing the implementation to make the ints wider
]as well? MPJ

I thought Dan already answered that. rand() returns a result in the
range 0 to RAND_MAX. Since the return type of rand() is int, RAND_MAX
must be representable as an int (at least 32767, and at most INT_MAX).
 
Ad

Advertisements

M

Merrill & Michele

Keith Thompson said:
Merrill & Michele said:
"Merrill & Michele"
[...]
In real world implementations, it is the value of RAND_MAX that is chosen
according to the properties of type int (among other criteria), and not
the other way round.
"Keith Thompson"
Agreed, with emphasis on "among other criteria". (Solaris has 32-bit
ints, but RAND_MAX==32767.)

The standard requires both INT_MIN and RAND_MAX to be at least 32767.
However, the type of RAND_MAX need not be int, only its value must be
in the range of the type int (it is defined as an integer constant
expression).

I suppose an implementation could define RAND_MAX with a type other
than int, but I can't think of any reason to do so other than sheer
perversity. I would expect it to be an integer-constant or some
simple expression, and it has to be within the range of type int.

It's generally not wise to snip the question and then beg it. MPJ

Um, what question did I beg?

I wasn't actually trying to answer the original question; I was
elaborating on a couple of points that Dan Pop had made.

If the question you're referring to is:

] Are you ever going to get in trouble with r declared as an int? Can
] RAND_MAX be 10^9 without forcing the implementation to make the ints wider
]as well? MPJ

I thought Dan already answered that. rand() returns a result in the
range 0 to RAND_MAX. Since the return type of rand() is int, RAND_MAX
must be representable as an int (at least 32767, and at most INT_MAX).
 
M

Merrill & Michele

Keith Thompson wrote: what question did I beg?

There isn't a 'what" when you beg the question. I claim that you assumed
your result in order to prove it. To schloppen. MPJ
 
K

Keith Thompson

Merrill & Michele said:
There isn't a 'what" when you beg the question. I claim that you assumed
your result in order to prove it. To schloppen. MPJ

Ok, what result did I assume? (I don't believe I assumed anything.)
 
M

Merrill & Michele

Ok, what result did I assume? (I don't believe I assumed anything.)

As usual, I must admit to having trouble reading my screen. Mr. Pop's
declaration that RAND_MAX must be within the int range was what was to be
shown. Begging the question is when A is to be shown, and the proof goes:
if A, then A, a tautology. Upon further review, the flag has been picked
up. San Diego will not be charged a time out. MPJ
 
R

Richard Bos

Tim Rentsch said:
Humorous footnote - I've seen the acronym HTH at the end of countless
articles by now, but I still don't know what it means. :)

Hail To Hastur - His Arrival Nears Daily.

HTH; HAND.

Richard
 
Ad

Advertisements

D

Dan Pop

In said:
[email protected] (Dan Pop) said:
%CC-I-ANACHRONISM, The "=-" operator is an obsolete form,
and may not be portable.
[...]
I guess many other people have been bitten by that and bitterly complained
until DEC introduced this *badly* needed warning in VAX C, otherwise the
nicest pre-ANSI C compiler I have ever used.

Probably so.

Strictly speaking it's an informational message ("-I-"), which is
weaker than a warning ("-W-"), which I think makes it easier to
suppress, but that shouldn't matter unless you're foolish enough to
turn off diagnostics.

The distinction between warnings and informational messages is something
specific to VMS and the only difference is that you get another warning
(or is it informational message?) when you link an object file that
generated warnings when compiled.

To my, any compiler-generated message is a diagnostic and I deliberately
ignore any difference between errors and warnings. OTOH, I have the
luxury to use a compiler that can be tuned to generate only those
diagnostics that I consider as deserving my attention (gcc -Wall -O).
The best solution for anyone still programming on VMS^H^H^H OpenVMS is
probably to use the newer DECC rather than VAXC.

I was not even aware that VAX C still exists as a supported HP product.

Dan
 
D

Dan Pop

In said:
[lots of snippage]

Then I also don't need to declare time_t timer, correct? I just need to
include time.h (?).

Right. No point in declaring a variable you never use.
I don't see how your code divides rand() into equiprobable events modulo
fam_size.

The non-equiprobability of rand() % FAMSIZ is so insignificant that it is
not worth worrying about, especially considering that it is not central
to the algorithm. No reasonable family size is going to get within one
order of magnitude of the smallest allowed value for RAND_MAX. What
really matters is that rand() % FAMSIZ generates a reasonably random
value in the 0 .. FAMSIZ-1 range.

If that is not good enough for you, use your own random generator (the
rand() coming with most implementations doesn't have a particularly
good reputation) and use the algorithm described in the FAQ for
mapping it into the integer interval of your choice.
This is for the printf( %d, when it should have been printf(%ld correct?

Right. Even if it works on your system, your printf call is not correct
and need not work on other systems. In the C jargon, it invokes undefined
behaviour.
I've got to figure something out for future posts. What I see and what I
post are radically different.

1. Convince your editor not to use TABs for indentation purposes.

2. If the above is not possible, write your own program to replace each
TAB character by as many spaces as needed to implement TAB stops every
N characters. It is an excellent exercise for you, at your current
level.
a random shift value in the range 1 .. FAMSIZ-1 and relocate each member
to position (i + shift) % FAMSIZ:

shift = rand() % (FAMSIZ-1) + 1;
for (i = 0; i < FAMSIZ; i++) members = (i + shift) % FAMSIZ;

The derangement is still perfect.


So is members = members[i+1] for the the way we initialized.


True, but the results are predictible, while my method picks one of
the FAMSIZ-1 trivial derangements at random.

Dan
 
Ad

Advertisements

M

Merrill & Michele

top_num=RAND_MAX-(RAND_MAX%fam_size)-1;
The non-equiprobability of rand() % FAMSIZ is so insignificant that it is
not worth worrying about, especially considering that it is not central
to the algorithm. No reasonable family size is going to get within one
order of magnitude of the smallest allowed value for RAND_MAX. What
really matters is that rand() % FAMSIZ generates a reasonably random
value in the 0 .. FAMSIZ-1 range.
If that is not good enough for you, use your own random generator (the
rand() coming with most implementations doesn't have a particularly
good reputation) and use the algorithm described in the FAQ for
mapping it into the integer interval of your choice.

You're amputating something that requires a band-aid. By throwing out rand
calls that exceed top_num above, the events mod FAMSIZ are equiprobable.
Period. I'll add white-spacing so as to make that statement clearer.
2. If the above is not possible, write your own program to replace each
TAB character by as many spaces as needed to implement TAB stops every
N characters. It is an excellent exercise for you, at your current
level.

Following one of your earlier posts, I determined that I can direct the
input on the command line of this platform which will not be named, and thus
get text in and out of these progs in a sensible manner. The conversion
might be six lines of code and is behind me in K&R.
a random shift value in the range 1 .. FAMSIZ-1 and relocate each member
to position (i + shift) % FAMSIZ:

shift = rand() % (FAMSIZ-1) + 1;
for (i = 0; i < FAMSIZ; i++) members = (i + shift) % FAMSIZ;

The derangement is still perfect.


So is members = members[i+1] for the the way we initialized.


True, but the results are predictible, while my method picks one of
the FAMSIZ-1 trivial derangements at random.


This statement contradicts a previous of yours. I'll test it to see which
Dan Pop is correct. MPJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top