Sunday, 25 January 2009

Generating a random number / seed

You may on occasion need to generate a random number within a shell script. Maybe you have 10 servers that run an update at 1:00 you do not want them all downloading at the same time so you end up staggering each server to download 10 minutes apart, this piece of code allows a number to be generated at random that can be used as a delay so one server connect after a 1 minute wait another at maybe 4 minutes another at perhaps 1.5 minutes etc. the point is the same script can be used on all machines with out having to worry about the scripts clashing.




# Function - generate a random number, range from 30 to 336,
# using time of day and hostname string as seeds
get_random()
{

random=`nawk -v maxnum=300 '
BEGIN {
# seed random number using time of day
srand()
# get a random number, range from 30 to 300
range = maxnum - 30
select = 30 + int(rand() * range)
print select
}'`

# generate a number as second seed from hostname string
# value ranges from 1 to 36
# this is needed to avoid different hosts from generating
# the same random number at the same time
seed2=`echo $MYHOST | nawk '
{
# create array from a list of lower case alphabetic and numeric
# characters. use array index number as value for a character
# the array size is 36 ( = 10 numeric + 26 alphabetic )
z=split("1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", num, ",")

str=tolower($0)
strlength=length($0)

# generate a number from given string, value range from 1 to 36
for (i = 1; i <= strlength; ++i){
char=substr(str,i,1)
for (j = 1; j <= z; ++j){
if (char == num[j] ){
sum+=j
count++
}
}
}
print int(sum/count)
}'`

# generate a random number using the second seed
# value ranges from 30 to 336
random=`expr $random + $seed2`

}





<pre class="postCode">



# Function - generate a random number, range from 30 to 336,

# using time of day and hostname string as seeds

get_random()

{



        random=`nawk -v maxnum=300 '

        BEGIN {

                # seed random number using time of day

                srand()

                # get a random number, range from 30 to 300

                range = maxnum - 30

                select = 30 + int(rand() * range)

                print select

        }'`



        # generate a number as second seed from hostname string

        # value ranges from 1 to 36

        # this is needed to avoid different hosts from generating

        # the same random number at the same time

        seed2=`echo $MYHOST | nawk '

        {

                # create array from a list of lower case alphabetic and numeric

                # characters. use array index number as value for a character

                # the array size is 36 ( = 10 numeric + 26 alphabetic )

                z=split("1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", num, ",")



                str=tolower($0)

                strlength=length($0)



                # generate a number from given string, value range from 1 to 36

                for (i = 1; i <= strlength; ++i){

                        char=substr(str,i,1)

                        for (j = 1; j <= z; ++j){

                                if (char == num[j] ){

                                        sum+=j

                                        count++

                                }

                        }

                }

                print int(sum/count)

        }'`



        # generate a random number using the second seed

        # value ranges from 30 to 336

        random=`expr $random + $seed2`



}



</pre>

No comments:

Post a Comment