- Subject: Re: [slang-users] generating random characters
- From: "Oleg Verych" <olecom@xxxxxxxxx>
- Date: Tue, 12 Feb 2008 13:30:01 +0100
On Feb 12, 2008 12:50 PM, Troy Piggins <troy_slang@xxxxxxxxx> wrote:
> I'm looking at a slang macro for slrn and I wanted to add 3 random characters
> to a string. Can't find a standard slang function that does this. I did find
> some random number generators in the gsl module, but am reluctant to use that
> because not sure how many "regular" slrn users would have extra slang modules
> installed.
>
> I was thinking as a workaround using the internal bash function $RANDOM which
> generates a random number between 0 and 32767. Then I was going to limit that
> to an upper limit of 26, convert that to a corresponding alphabetical char and
> do that 3 times. Seems tedious.
>
> I think for the purposes of what I need I could get away with just numbers
> instead of alphabetical chars and skip the step of upper limit 26, convert to
> char, and repeat 3 times. I could just set an upper limit of 999 and take a
> random number.
>
> Here's what I came up with:
>
> variable rnd= system( "RND=$RANDOM; let 'RND %= 999'; echo $RND");
>
> Trouble is it is always outputting 0.
maybe it's return code, that matters?
(Just some general stuff)
olecom@flower:/tmp/blinux$ a() { RND=$RANDOM; let 'RND %= 999'; return $RND; }
olecom@flower:/tmp/blinux$ a
olecom@flower:/tmp/blinux$ $?
bash: 181: command not found
olecom@flower:/tmp/blinux$ a
olecom@flower:/tmp/blinux$ $?
bash: 128: command not found
olecom@flower:/tmp/blinux$ a
olecom@flower:/tmp/blinux$ $?
bash: 250: command not found
olecom@flower:/tmp/blinux$
[]
> 1. What am I doing wrong above?
> 2. Is there a better way to do this with standard slang?
> 3. Is there a simple way to generate random alpha chars?
olecom@flower:/tmp/blinux$ dd bs=1 count=1 </dev/urandom | od
1+0 records in
1+0 records out
1 byte (1 B) copied, 2.9e-05 seconds, 34.5 kB/s
0000000 000057
0000001
olecom@flower:/tmp/blinux$ dd bs=1 count=1 </dev/urandom | od
1+0 records in
1+0 records out
1 byte (1 B) copied, 2.8e-05 seconds, 35.7 kB/s
0000000 000232
0000001
olecom@flower:/tmp/blinux$
--
-o--=O`C
#oo'L O
<___=E M
[2008 date index]
[2008 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]