Sponsored Content
Top Forums Shell Programming and Scripting Problem using function in awk Post 302451078 by durden_tyler on Sunday 5th of September 2010 03:16:25 PM
Old 09-05-2010
Another thing to note is that the logic of these functions is such that they return the same values for different arguments.

Code:
$ 
$ 
$ awk '
function rgaussian1(r1, r2)
{      pi = 3.142
      v1 = sqrt( -2 * log(rand()) )
      v2 = 2 * pi * rand()
      r1 = a * sin(b)
      r2 = a * cos(b)
      print r1, r2
}
function rgaussian2(r1, r2)
{
      do {
          v1 = 2 * rand() - 1
          v2 = 2 * rand() - 1
          rsq = v1 * v1 + v2 * v2
      } while (rsq > 1)
      fac = sqrt(-2 * log(rsq) / rsq)
      r1 = v2 * fac
      r2 = v1 * fac
      print r1, r2
}
BEGIN {
  rgaussian1(200, 200);
  rgaussian2(0.1254544647, 0.57385902382745)
}'
0 0
-0.196896 0.195777
$ 
$ 

With some diagnostics added:

Code:
$ 
$ awk '
function rgaussian1(r1, r2)
{
  print "BEGIN rgaussian1 =>",r1, r2, r1+r2, r1*r2;
  pi = 3.142
  v1 = sqrt( -2 * log(rand()) )
  v2 = 2 * pi * rand()
  r1 = a * sin(b)
  r2 = a * cos(b)
  #print r1, r2
  print "END rgaussian1 =>",r1, r2, r1+r2, r1*r2;
}
function rgaussian2(r1, r2)
{
  print "BEGIN rgaussian2 =>",r1, r2, r1+r2, r1*r2;
  do {
    v1 = 2 * rand() - 1
    v2 = 2 * rand() - 1
    rsq = v1 * v1 + v2 * v2
  } while (rsq > 1)
  fac = sqrt(-2 * log(rsq) / rsq)
  r1 = v2 * fac
  r2 = v1 * fac
  #print r1, r2;
  print "END rgaussian2 =>",r1, r2, r1+r2, r1*r2;
}
BEGIN {
  rgaussian1(100, 100);
  rgaussian2(0.1254544647, 0.57385902382745)
}'
BEGIN rgaussian1 => 100 100 200 10000
END rgaussian1 => 0 0 0 0
BEGIN rgaussian2 => 0.125454 0.573859 0.699313 0.0719932
END rgaussian2 => -0.196896 0.195777 -0.00111973 -0.0385477
$ 
$ 

tyler_durden
 

10 More Discussions You Might Find Interesting

1. Programming

Problem with aio_write() function

Hello, How to execute a call back function after aio_write() or aio_read() in Sun Solaris 5.7? I have filled the control block struct aiocb as follows: aio_sigevent.sigev_signo = SIGEV aio_sigevent.sigev_notify = SIGEV_THREAD Then I have filled the call back function in ... (0 Replies)
Discussion started by: hmurali
0 Replies

2. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies

3. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

4. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

5. Shell Programming and Scripting

splice function problem

Hi All, I am using splice function in for loop to delete particular element from array with one condition. my $cnt=0; foreach my $elem (@result) { if (condition){ splice(@result, $cnt, 1);} else{ $cnt++;} } Now when in array, two elements comes sequentially with the... (3 Replies)
Discussion started by: gentleDean
3 Replies

6. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

7. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

8. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

9. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies

10. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
RAND(3) 						   BSD Library Functions Manual 						   RAND(3)

NAME
rand, rand_r, srand, sranddev -- bad random number generator LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdlib.h> int rand(void); int rand_r(unsigned *seed); void srand(unsigned seed); void sranddev(void); DESCRIPTION
These interfaces are obsoleted by arc4random(3). The rand() function computes a sequence of pseudo-random integers in the range of 0 to RAND_MAX (as defined by the header file <stdlib.h>). The srand() function sets its argument seed as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the functions are automatically seeded with a value of 1. The sranddev() function initializes a seed, using the random(4) random number device which returns good random numbers. However, the rand() function still remains unsuitable for cryptographic use. The rand_r() function provides the same functionality as rand(). A pointer to the context value seed must be supplied by the caller. SEE ALSO
arc4random(3), random(3), random(4) STANDARDS
The rand() and srand() functions conform to ISO/IEC 9899:1990 (``ISO C90''). The rand_r() function is as proposed in the POSIX.4a Draft #6 document. BSD
May 25, 1999 BSD
All times are GMT -4. The time now is 08:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy