Lloyd Linklater wrote:
> function GetRandomChar: char;
> var
>   r: integer;
> begin
>   r := random(36);
>   case r of
>     0..25: result := chr(ord('a') + r);
>     else : result := chr(ord('0') + r);
>   end;
> end;

In this case, you can just do:

def get_random_char
  rand(36).to_s(36)
end

Since Ruby allows arbitrary bignums, you can also get strings this way 
too. e.g. for an 8-digit string:

    rand(36 ** 8).to_s(36)

However there's a bug there, because numbers with one or more leading 
zeros will be truncated. How to left-pad a non-decimal number with zeros 
isn't actually that obvious. Maybe someone can point out something 
simpler than this:

    ("0"*8 + rand(36 ** 8).to_s(36))[-8..-1]

(Unfortunately, "%08s" as a format string pads with spaces not zeros)
-- 
Posted via http://www.ruby-forum.com/.