slang-users mailing list

[2008 Date Index] [2008 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

[slang-users] generating random characters


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.

If I use this:

variable rnd= system( "RND=$RANDOM; let 'RND %= 999'");

it always outputs 35212.

2 questions:

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?

-- 
Troy Piggins



[2008 date index] [2008 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]