-
Notifications
You must be signed in to change notification settings - Fork 93
/
rand-src.c
86 lines (69 loc) · 1.77 KB
/
rand-src.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* RAND-SRC.C - Generate random message bits. */
/* Copyright (c) 1995-2012 by Radford M. Neal.
*
* Permission is granted for anyone to copy, use, modify, and distribute
* these programs and accompanying documents for any purpose, provided
* this copyright notice is retained and prominently displayed, and note
* is made of any changes made to these programs. These programs and
* documents are distributed without any warranty, express or implied.
* As the programs were written for research purposes only, they have not
* been tested to the degree that would be advisable in any important
* application. All use of these programs is entirely at the user's own
* risk.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "open.h"
#include "rand.h"
void usage(void);
/* MAIN PROGRAM. */
int main
( int argc,
char **argv
)
{
int seed, bs, nb;
char *file, *n_bits;
char junk;
int i, j;
FILE *f;
if (!(file = argv[1])
|| !argv[2] || sscanf(argv[2],"%d%c",&seed,&junk)!=1
|| !(n_bits = argv[3])
|| argv[4])
{ usage();
}
if (sscanf(n_bits,"%d%c",&nb,&junk)==1)
{ if (nb<=0) usage();
bs = 1;
}
else if (sscanf(n_bits,"%dx%d%c",&bs,&nb,&junk)==2)
{ if (nb<=0 || bs<=0) usage();
}
else
{ usage();
}
f = open_file_std(file,"w");
if (f==NULL)
{ fprintf(stderr,"Can't create source file: %s\n",file);
exit(1);
}
rand_seed(10*seed+2);
for (i = 0; i<nb; i++)
{ for (j = 0; j<bs; j++)
{ fprintf(f,"%d",rand_int(2));
}
fprintf(f,"\n");
}
if (ferror(f) || fclose(f)!=0)
{ fprintf(stderr,"Error writing random source blocks to %s\n",file);
exit(1);
}
return 0;
}
/* PRINT USAGE MESSAGE AND EXIT. */
void usage(void)
{ fprintf(stderr,"Usage: rand-src source-file seed n-bits\n");
exit(1);
}