List Info

Thread: how to replace $RANDOM




how to replace $RANDOM
user name
2007-03-21 17:14:59
Hi,

I'm trying to fix the subversion-base package's dependency
on $RANDOM.
It causes pkgsrc to complain about portability (rightfully
so).

In this particular case, all I need is to generate a random
integer.
Could probably even use the current process ID for this
scenario, but
I'd like to know how to generate a `random' number from
userland,
without having to write a program.

How can I get the same behaviour as $RANDOM in a portable
way?

>From the bash man page:

RANDOM Each time this parameter is referenced, a random
integer between
       0 and 32767 is generated.  The sequence of random
numbers may be
       initialized by assigning a value to RANDOM.  If
RANDOM is unset,
       it loses its special properties,  even  if  it  is 
subsequently
       reset.



Louis

Re: how to replace $RANDOM
country flaguser name
United States
2007-03-21 19:15:05
> How can I get the same behaviour as $RANDOM in a
portable way?

I saw another example in pkgsrc that just uses $$.

Here are some ideas:

http://
home.comcast.net/~j.p.h/cus-faq.html#2

http://lin
uxgazette.net/issue55/tag/4.html

http://w
ww.shelldorado.com/scripts/cmds/rand

Also jot -r does random but that is not portable.

And our NetBSD ksh has $RANDOM also.

Re: how to replace $RANDOM
country flaguser name
United States
2007-03-21 20:47:08
Jeremy C. Reed wrote:
>> How can I get the same behaviour as $RANDOM in a
portable way?
> 
> I saw another example in pkgsrc that just uses $$.
> 
> Here are some ideas:
> 
> http://
home.comcast.net/~j.p.h/cus-faq.html#2
> 
> http://lin
uxgazette.net/issue55/tag/4.html
> 
> http://w
ww.shelldorado.com/scripts/cmds/rand
> 
> Also jot -r does random but that is not portable.
> 
> And our NetBSD ksh has $RANDOM also.


Thanks. I think that using $$ is appropriate here. The patch
below fixes
complaints from pkgsrc about portability in subversion-base.
$RANDOM was
only used to choose a random port number, so I used (( $$ %
32768 )) in
its place. I'm going to post the patch on the
Subversion-users mailing
list and see if anyone says anything. Otherwise we could
maybe put the
patch in pkgsrc??

Louis



--- subversion/tests/cmdline/davautocheck.sh.orig      
2007-03-21
22:01:10.000000000 +0000
+++ subversion/tests/cmdline/davautocheck.sh    2007-03-21
22:24:01.000000000 +0000
 -62,7
+62,7 
   echo -n "$SCRIPT: $1 (y/n)? [$2] "
   read -n 1 -t 32
   echo
-  [ "${REPLY2}" ==
'y' ]
+  [ "${REPLY2}" =
'y' ]
 }

 function get_loadmodule_config() {
 -170,7
+170,7 
     || fail "Authz_User module not found."
 }

-HTTPD_PORT=$(($RANDOM+1024))
+HTTPD_PORT=$((($$ % 32768 ) +1024))
 HTTPD_ROOT="$ABS_BUILDDIR/subversion/tests/cmdline/htt
pd-$(date
'+%Y%m%d-%H%M%S')"
 HTTPD_CFG="$HTTPD_ROOT/cfg"
 HTTPD_PID="$HTTPD_ROOT/pid"
 -268,7
+268,7 

 say "HTTPD is good, starting the tests..."

-if [ $# == 0 ]; then
+if [ $# = 0 ]; then
   time make check "BASE_URL=$BASE_URL"
   r=$?
 else


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )