1834149 Members
2852 Online
110064 Solutions
New Discussion

random number

 
SOLVED
Go to solution
jim bidebo
Regular Advisor

random number

im looking for a way to generate a random number. i dont want to add any packages except for the base installation of hp-ux. if theres no way to do it, i have access to gcc, but my C knowledge is quite limited. so help with an easy random number generator in c would also be apriciated.
3 REPLIES 3
Stefan Farrelly
Honored Contributor

Re: random number

You can do it with perl, which comes with HP-UX;

# random number from 0 to 255
perl -e 'srand(time() ^ $$); while (1) { print int(rand(1)*256); }' | dd bs=8k of=/tmp/tt count=100
Im from Palmerston North, New Zealand, but somehow ended up in London...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: random number

It couldn't be simpler, in the POSIX or Korn shell, ${RANDOM} is your boy.

You can also do things like
RAND=$((${RANDOM} % 100)) to restrict to a rangle 0-99.

There is also a more robust RNG in perl that is trivially easy to use.
If it ain't broke, I can fix that.
Hai Nguyen_1
Honored Contributor

Re: random number

Jim

There is an environment varialbe named RANDOM. Each time, each time you run it, you have a different number.
Try this at a comman prompt several times to see for yourself:

# echo $RANDOM

Hope this helps.

Hai