If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > random numbers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-24-04, 14:50
alexandra84 alexandra84 is offline
Registered User
 
Join Date: Aug 2004
Posts: 6
random numbers

I am writing a unix shell script that will generate random numbers each time through a loop to store in two shell variables, which are then used later by a different program. The problem is, I can't seem to get the random numbers to generate correctly! I am not very experienced using the awk language, is there anyway i can call rand from awk to generate numbers between 0 and n, which are then stored in a variable? Or am I going about this the wrong way?
Reply With Quote
  #2 (permalink)  
Old 08-24-04, 15:46
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
If it's a shell script, use the shell variable $RANDOM to create your random number. Use the modulus operator (%) if you want your values within a range.

e.g.

To generate a random number between 0 and 9 use...

myRandomNumber=$((RANDOM%10))

If it's an awk script, you will need to 'seed' your random number initially using 'srand()'. The function rand() will then generate a random number between 0 and 1, so if you want a number between 0 and 9 (inclusive), multiply the value by 10. Use the int() function or sprintf or whatever to attain an integer

e.g.

awk 'BEGIN {srand();myRandomNumber=sprintf("%d",rand()*10); print myRandomNumber}'

awk 'BEGIN {srand();myRandomNumber=int(*10); print myRandomNumber}'

HTH

Last edited by Damian Ibbotson; 08-24-04 at 16:08.
Reply With Quote
  #3 (permalink)  
Old 08-24-04, 16:06
alexandra84 alexandra84 is offline
Registered User
 
Join Date: Aug 2004
Posts: 6
Thanks for the help... but thats about what I've been finding on other threads and it's not working.

I can't use $RANDOM because I'm using a c-shell. (I tried to convert my script to bash but that was a whole other headache so I'm pursuing this first).

Perhaps I'm being stupid (or inexperienced, or both), but the awk lines you wrote do not work when i try to insert them in my code or type them into a command prompt.

Is there an easier way to call a random number in a c-shell than awk scripting? Something along the lines of using $RANDOM in the bash shells?

Thanks again for any help
Reply With Quote
  #4 (permalink)  
Old 08-24-04, 16:58
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
try something like this:

# on Solaris
nawk 'BEGIN {srand();myRandomNumber=int(rand()*10); print myRandomNumber}'

OR

/usr/xpg4/bin/awk 'BEGIN {srand();myRandomNumber=int(rand()*10); print myRandomNumber}'
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 08-24-04, 17:13
alexandra84 alexandra84 is offline
Registered User
 
Join Date: Aug 2004
Posts: 6
Thanks, the first thing works.

HOWEVER... my main problem I guess is that I don't want to just print the random number. I want to assign it to a variable I have previously declared in my script.

here's the area w/ the problem:

#!/bin/csh
...
set xcenter
set ycenter

while ($i < 1000)
nawk 'BEGIN {srand();xcenter=int(rand()*5000)}'
nawk 'BEGIN {srand();ycenter=int(rand()*5000)}'
echo $xcenter
echo $ycenter
...
and then I need to use these two variables in following programs I call, which part of the script is working fine. The problem is assigning the variables w/ diff random numbers each time through the loop.
I've tried (aimlessly) to pipe the outcome of the awk lines, but I think I'm blindly looking for a way to do this. Any help is GREATLY appreciated!
Reply With Quote
  #6 (permalink)  
Old 08-24-04, 17:20
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
not being a big fan of csh (to say the least). but try this:

Code:
#!/bin/csh
...
set xcenter
set ycenter

while ($i < 1000)
   set xcenter=`nawk 'BEGIN {srand();print int(rand()*5000)}'`
   set ycenter=`nawk 'BEGIN {srand();print int(rand()*5000)}'`
   echo $xcenter
   echo $ycenter
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #7 (permalink)  
Old 08-25-04, 13:16
alexandra84 alexandra84 is offline
Registered User
 
Join Date: Aug 2004
Posts: 6
Talking

THANKS! it worked finally.

i hate csh too, never again will i script in it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On