I've been looking all over, and I can't seem to figure this out. I have to generate a random number within the range of -99 and 99. I know about RANDOM, but I don't know how I could use that to keep the numbers within this range. Any help is greatly appreciated.


I have looked at that thread before, didn't find it was of much help.

I have looked at that thread before, didn't find it was of much help.

How could it not be? Do you honestly need someone to tell you how to access an environment variable?
( echo $RANDOM )
Or maybe how to store it's results?
( varName=`echo $RANDOM` )

If you want to restrict it to certain numberset, just frame floor and ceiling variables.

For instance $RANDOM/600

will give you an approximate range of 0 to 60 (or 59, I forget).

If you just fool around with that for a while, you should be able to tackle it. My math may be wrong too, because the max my RANDOM will produce may differ from yours.

Best wishes,


Thanks. I understand how to set a range from say, 0 - 99 as you mentioned. Does anyone have any suggestions as to how I can set a negative lower limit. I need to have a range from -99, 99. I appreciate any help.

well I figured it out. If I do something where I set the range from 0, 198, then I can subtract 99 from that, and it should give me the range from -99, 99. So I think something like:

($RANDOM % 198) - 99

Good idea :)

Just remember to use "/" for division rather than "%" which will give you the modulus (remainder) of the division.

, Mike


It's not like you wouldn't have figured it out soon enough anyway ;)

Congrats on coming up with a creative way to solve your problem!

< Mike

Don't forget to initialize the sequence of random numbers by assigning a numeric value to RANDOM before you start retrieving them. Otherwise you may get the same set of numbers each time. See the ksh man page.

This article has been dead for over six months. Start a new discussion instead.