random letter generator

Karl Heuer karl at haddock.ima.isc.com
Mon Oct 22 13:33:19 AEST 1990


In article <1990Oct19.154904.9020 at hellgate.utah.edu> msmith%peruvian.utah.edu at cs.utah.edu (Matthew Smith) writes:
>In article <1990Oct17.120817.4636 at swbatl.sbc.com> uucigj at swbatl.sbc.com writes:
>>I am in need  of a function that will return a random captial letter
>>(A-Z).  This seems like an easy one, but it is eluding me.  Any help?
>>
>>Please email since I don't get a chance to read this group that often.
>
>How about generating a random number between 0 and 25, and adding 65 to it.
>That would give you a range of 65 to 90, or ascii codes for A and Z.
>
>num=25*random()+65;

I see four problems with your response.

Firstly, the multiplier should be 26 rather than 25.  The fact that you made
this mistake suggests that you're thinking in origin one and translating to
C's origin zero.  If you think in origin zero and half-open intervals in the
first place, the constant 25 never appears: you need to select from 26 items,
so you need a random integer in [0,26), so you multiply by 26.

Secondly, you're using magic numbers that are ASCII-specific.  Even if you
want to simplify the problem by ignoring noncontiguous character sets,
there's no reason to say 65 when you mean 'A'.  This isn't BASIC.

Thirdly, you used a non-standard function random(), which apparently is
supposed to return a real number in [0.0,1.0).  rand() is more likely to
exist (in particular, it's in ANSI C).

Fourthly, you ignored the request to reply by mail.

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list