#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 5_8.cmp 5_8A1.h 5_8a2.c 5_8a3.c 5_8a4.c 5_8a5.h 5_8b1.h 5_8b2.c 5_8b3.c 5_8tst.c makefile tst.c # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 23:04:20 EDT 1990 # echo x - 5_8.cmp sed 's/^X//' > 5_8.cmp << '!EOF!' draw(): 1103527590 draw(): 377401575 draw(): 662824084 draw(): 1147902781 draw(): 2035015474 draw(): 368800899 draw(): 1508029952 draw(): 486256185 draw(): 1062517886 draw(): 267834847 draw(): 180171308 draw(): 836760821 draw(): 595337866 draw(): 790425851 draw(): 2111915288 draw(): 1149758321 draw(): 1644289366 draw(): 1388290519 draw(): 1647418052 draw(): 1675546029 fdraw(): 0.51387 fdraw(): 0.175741 fdraw(): 0.308652 fdraw(): 0.534534 fdraw(): 0.947628 fdraw(): 0.171736 fdraw(): 0.702231 fdraw(): 0.226431 fdraw(): 0.494773 fdraw(): 0.12472 fdraw(): 0.0838988 fdraw(): 0.389647 fdraw(): 0.277226 fdraw(): 0.368071 fdraw(): 0.983437 fdraw(): 0.535398 fdraw(): 0.765682 fdraw(): 0.646473 fdraw(): 0.767139 fdraw(): 0.780237 ur.draw(): 40 ur.draw(): 23 ur.draw(): 30 ur.draw(): 41 ur.draw(): 62 ur.draw(): 23 ur.draw(): 50 ur.draw(): 26 ur.draw(): 39 ur.draw(): 21 ur.draw(): 19 ur.draw(): 34 ur.draw(): 28 ur.draw(): 33 ur.draw(): 64 ur.draw(): 41 ur.draw(): 53 ur.draw(): 47 ur.draw(): 53 ur.draw(): 54 er.draw(): 56 er.draw(): 22 er.draw(): 46 er.draw(): 30 er.draw(): 32 er.draw(): 60 er.draw(): 40 er.draw(): 35 er.draw(): 45 er.draw(): 54 er.draw(): 61 er.draw(): 58 er.draw(): 58 er.draw(): 48 er.draw(): 52 er.draw(): 44 er.draw(): 34 er.draw(): 32 er.draw(): 25 er.draw(): 56 !EOF! ls -l 5_8.cmp echo x - 5_8A1.h sed 's/^X//' > 5_8A1.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* the result of randint() is always >= 0 */ /* uniform distribution in the interval [0,MAX] */ class randint { long randx; public: randint(long s = 0) { randx = s; } void seed(long s) { randx = s; } int draw(); float fdraw(); }; !EOF! ls -l 5_8A1.h echo x - 5_8a2.c sed 's/^X//' > 5_8a2.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ randx = randx * 1103515245 + 12345; !EOF! ls -l 5_8a2.c echo x - 5_8a3.c sed 's/^X//' > 5_8a3.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ return randx & LONG_MAX; !EOF! ls -l 5_8a3.c echo x - 5_8a4.c sed 's/^X//' > 5_8a4.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ return (randx & LONG_MAX) / (float) LONG_MAX; !EOF! ls -l 5_8a4.c echo x - 5_8a5.h sed 's/^X//' > 5_8a5.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Uniform distribution in the interval [0,LONG_MAX) // and [0,1). #include class randint { long randx; void getx() { randx = randx * 1103515245 + 12345; } public: randint(long s = 0) { randx = s; } void seed(long s) { randx = s; } int draw() { getx(); return randx & LONG_MAX; } float fdraw() { getx(); return (randx & LONG_MAX) / (float) LONG_MAX; } }; !EOF! ls -l 5_8a5.h echo x - 5_8b1.h sed 's/^X//' > 5_8b1.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // uniform distribution in the interval [low,high) struct urand : public randint { int low, high; urand(int l, int h) { low = l; high = h; } int draw(); }; // exponential distribution random number generator struct erand : public randint { int mean; erand(int m) { mean = m; }; int draw(); }; !EOF! ls -l 5_8b1.h echo x - 5_8b2.c sed 's/^X//' > 5_8b2.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ int urand::draw() { return (int) (low + (high - low) * randint::fdraw()); } !EOF! ls -l 5_8b2.c echo x - 5_8b3.c sed 's/^X//' > 5_8b3.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include int erand::draw() { return (int) (-mean * log( 1.0 - randint::fdraw() ) + 0.5); } !EOF! ls -l 5_8b3.c echo x - 5_8tst.c sed 's/^X//' > 5_8tst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include "5_8a5.h" // class randint #include "5_8b1.h" // class urand, erand #include "5_8b2.c" // urand::draw() #include "5_8b3.c" // erand::draw() main() { randint r(1); for (int i = 0; i++ < 20; ) cout << "draw(): " << r.draw() << "\n"; r.seed(1); for (i = 0; i++ < 20; ) cout << "fdraw(): " << r.fdraw() << "\n"; urand ur(15, 65); ur.seed(1); for (i = 0; i++ < 20; ) cout << "ur.draw(): " << ur.draw() << "\n"; erand er(50); er.seed(1); for (i = 0; i++ < 20; ) cout << "er.draw(): " << ur.draw() << "\n"; return 0; } !EOF! ls -l 5_8tst.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: 5_8tst 5_8tst: 5_8a5.h 5_8b1.h 5_8b2.c 5_8b3.c 5_8tst.c $(CC) 5_8tst.c -o 5_8tst -lm CMP= 5_8.cmp OUT= 5_8.out 5_8.out: 5_8tst ; 5_8tst > 5_8.out test: all $(OUT) $(CMP) cmp 5_8.out 5_8.cmp echo tests done !EOF! ls -l makefile echo x - tst.c sed 's/^X//' > tst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include !EOF! ls -l tst.c # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0