1834461 Members
2848 Online
110067 Solutions
New Discussion

Re: puzzle no - 2

 
SOLVED
Go to solution
U.SivaKumar_2
Honored Contributor

puzzle no - 2

Hi,
Can anybody write a shell script which can
generate random numbers and append to a output
file ?.

CONDITION: Should NOT use echo $RANDOM

regards,
U.SivaKumar
Innovations are made when conventions are broken
4 REPLIES 4
Paula J Frazer-Campbell
Honored Contributor

Re: puzzle no - 2

HI

Try these:-

Get a randon number in scripts
NOTE: produce a number 0 - $LIMIT-1
nawk:
set num=`nawk 'BEGIN { srand();
printf "%d", rand()*$LIMIT
}' /dev/null`

date:
set date=`date +%j%H%M%S`
set num=`expr $date \% $LIMIT`

(k/z/ba)sh
$RANDOM produces a number from 0.0 to 1.0
-----------------------------------------------------
Use a psudo random number generator
#!/bin/csh -f
#
# Random limit -- produce random number from 1 to limit
#
set multiplier = 25173
set increment = 13849
set modulus = 65536
set seedfile = $HOME/.rnd # seed file to use

if ( ! -f $seedfile ) then
echo '17' > $seedfile
endif

@ number = ( `cat $seedfile` * $multiplier + $increment ) % $modulus
echo $number > $seedfile

@ number = $number % $1 + 1
echo $number



Paula
If you can spell SysAdmin then you is one - anon
Stefan Farrelly
Honored Contributor

Re: puzzle no - 2


perl -e 'srand(time()^$$);while (1){print int(rand(1)*255);}'

This will print random numbers from 1-255 seeded by the current time when invoked, you can redirect output to a file. You will need perl installed though.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Tom Danzig
Honored Contributor

Re: puzzle no - 2

echo "" | awk '{srand();printf("%3d\n",rand()*100)}' >> /tmp/randfile
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: puzzle no - 2

Okay, pure POSIX or Korn

OUTFILE="myfile.out"
RANDY=$((${$} + ${SECONDS}))
while [[ 1 -eq 1 ]]
do
RANDY=$(((${RANDY} * 13107) + 25171))
echo ${RANDY} >> ${OUTFILE}
done

If it ain't broke, I can fix that.