--000e0cd6ac30e43377046d7e6500
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

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 : andom(36);
>  case r of
>    0..25: result : hr(ord('a') + r);
>    else : result : hr(ord('0') + r);
>  end;
> end;
>

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

def get_random_char
  (r  and(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

--000e0cd6ac30e43377046d7e6500--