Results 1 to 7 of 7

Thread: random numbers

  1. #1
    Join Date
    Aug 2004
    Posts
    6

    Unanswered: 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?

  2. #2
    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 17:08.

  3. #3
    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

  4. #4
    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> |
    +-----------------------+

  5. #5
    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!

  6. #6
    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> |
    +-----------------------+

  7. #7
    Join Date
    Aug 2004
    Posts
    6

    Talking

    THANKS! it worked finally.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •