Hi,
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.

Thanks

Recommended Answers

Answered by jbennet 1,618 in a post from
Answered by masijade 1,351 in a post from

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` )

Jump to Post
Answered by eggi 92 in a post from

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 …

Jump to Post

All 10 Replies

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,

Mike

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

oh yeah, my mistake, I don't know why I put modulus

np,

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.