#!/bin/csh -f #+ # grand -- generate a normal (Gaussian) random value # # Purpose: # Returns a normally-distributed (i.e., drawn from a Gaussian # normal distribution with mean of 0 and stddev of 1), using as # a seed either the argument provided or, if none, the time of day. # # Usage: # grand [seed] # # Arguments: # seed = INTEGER seed value for random number generator # # Exit values: # 0 = normal completion # 1 = wrong number of arguments # # Example: # 1) Return random normal values: # punaluu{lris}180: ./grand # 0.564412 # punaluu{lris}181: ./grand # -0.802072 # Note that the values differ because the process number (used # by default as the random generator's seed) changed. # # 2) Return a random value for a given seed: # punaluu{lris}183: ./grand 12345 # 1.08078 # punaluu{lris}184: ./grand 12345 # 1.08078 # Note that the values are the same when the same seed is used. #- # Modification history: # 2000-Jul-03 GDW Original version #----------------------------------------------------------------------- # parse command line args... if ( $#argv == 0 ) then @ seed = $$ # use Unix process number else if ( $#argv == 1 ) then @ seed = $1 # use argument else echo "Usage: grand [seed]" exit 1 endif gawk "BEGIN{dum = srand(${seed}) ; \ pi = atan2(0,-1) ; \ print sqrt(-2.*log(rand()))*cos(2.*pi*rand())}"