On Mon, Jun 29, 2009 at 5:18 PM, Lloyd Linklater <lloyd / 2live4.com> wrote:

> I have been trying to generate a random string.  One approach in, say,
> pascal would be something like this:
>
> 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;
>

'Translating' your Pascal program I'd do something like this:

def get_random_char
  (r = rand(36)) < 26 ? (?a+r).chr : (?0+r-26).chr
end

10.times { puts get_random_char} # => 10

# >> 5
# >> x
# >> p
# >> k
# >> s
# >> x
# >> d
# >> w
# >> 9
# >> t

-- 
Il pinguino ha rubato il mio lanciafiamme.

Blog: http://citizen428.net/
Twitter: http://twitter.com/citizen428