[comp.lang.c] random letter generator

uucigj@gandalf.sbc.com (Gregg Jensen) (10/17/90)

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.

      Gregg Jensen
---------------------------------------------------------------------- 
 These opinions are my own and do not necessarily reflect my companies.
      Southwestern Bell Telephone
---------------------------------------------------------------------- 

msmith%peruvian.utah.edu@cs.utah.edu (Matthew Smith) (10/20/90)

In article <1990Oct17.120817.4636@swbatl.sbc.com> uucigj@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.
>
>      Gregg Jensen

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;


Matt Smith

karl@haddock.ima.isc.com (Karl Heuer) (10/22/90)

In article <1990Oct19.154904.9020@hellgate.utah.edu> msmith%peruvian.utah.edu@cs.utah.edu (Matthew Smith) writes:
>In article <1990Oct17.120817.4636@swbatl.sbc.com> uucigj@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@ima.isc.com or uunet!ima!karl), The Walking Lint