LALApps 10.1.0.1-eeff03c
random_bank.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Duncan Brown
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20#include <stdio.h>
21#include <stdlib.h>
22
23extern void srand48 (long int);
24extern double drand48 (void);
25
26int main ( int argc, char *argv[] )
27{
28 int i, ntmplts;
29 double mmin, mmax;
30 FILE *fp = NULL;
31 FILE *bankfp = NULL;
32 FILE *txtfp = NULL;
33 long int seed = 0;
34
35 if ( argc != 4 )
36 {
37 fprintf( stderr, "useage: %s [mimumum mass] [maximum mass] [number of "
38 "templates]\n", argv[0] );
39 exit( 1 );
40 }
41
42 if ( (mmin = atof( argv[1] )) < 0 || (mmax = atof( argv[2] )) < 0 )
43 {
44 fprintf( stderr, "template masses must be positive.\n" );
45 exit( 1 );
46 }
47 if ( (ntmplts = atoi( argv[3] )) < 1 )
48 {
49 fprintf( stderr, "number of templates must be greater than zero.\n" );
50 exit( 1 );
51 }
52
53 /*
54 * Note: /dev/random can be slow after the first few accesses, which is why we're using urandom instead.
55 * [Cryptographic safety isn't a concern here at all]
56 */
57 if ( ! (fp = fopen( "/dev/urandom", "r" )) )
58 {
59 perror( "could not open /dev/urandom" );
60 exit ( 1 );
61 }
62
63 for ( i = 0; i < 8; ++i )
64 {
65 long int rbyte = (long int) fgetc( fp );
66 seed += rbyte << ( i * 8 );
67 }
68
69 fclose( fp );
70
71 srand48( seed );
72
73 if ( ! (bankfp = fopen( "bank.ilwd", "w" )) )
74 {
75 perror( "could not open bank.ilwd" );
76 exit ( 1 );
77 }
78
79 if ( ! (txtfp = fopen( "bank.txt", "w" )) )
80 {
81 perror( "could not open bank.txt" );
82 exit ( 1 );
83 }
84
85 fprintf( bankfp, "<?ilwd?>\n"
86 "<ilwd name='tmpltBank::sequence' comment='seed:%ld' size='7'>\n"
87 "<lstring name='real:domain' size='4'>TIME</lstring>\n"
88 "<int_4u name='gps_sec:start_time' units='sec'>0</int_4u>\n"
89 "<int_4u name='gps_nan:start_time' units='nanosec'>0</int_4u>\n"
90 "<int_4u name='gps_sec:stop_time' units='sec'>0</int_4u>\n"
91 "<int_4u name='gps_nan:stop_time' units='nanosec'>0</int_4u>\n"
92 "<real_8 name='time:step_size' units='sec'>1.0e+00</real_8>\n"
93 "<real_8 dims='2,%d' name='data' ndim='2' units='mass,mass'>",
94 seed, ntmplts );
95 fprintf( txtfp, "#seed = %ld\n", seed );
96
97 for ( i = 0; i < 2 * ntmplts; ++i )
98 {
99 double mass = mmin + ( mmax - mmin ) * drand48();
100 fprintf( bankfp, "%e", mass );
101 fprintf( txtfp, "%e", mass );
102 if ( i % 2 )
103 {
104 fprintf( txtfp, "\n" );
105 }
106 else
107 {
108 fprintf( txtfp, " " );
109 }
110 if ( i != (2 * ntmplts - 1) )
111 {
112 fprintf( bankfp, " " );
113 }
114 }
115
116 fprintf( bankfp, "</real_8>\n</ilwd>" );
117
118 fclose( bankfp );
119 fclose( txtfp );
120
121 exit( 0 );
122
123}
#define fprintf
int i
Definition: inspinj.c:596
int
seed
int main(int argc, char *argv[])
Definition: random_bank.c:26
void srand48(long int)
double drand48(void)
FILE * fp