]> matita.cs.unibo.it Git - pkg-cerco/frama-c-cost-plugin.git/blob - plugin/tests/success/random.c
Imported Upstream version 0.1
[pkg-cerco/frama-c-cost-plugin.git] / plugin / tests / success / random.c
1 /*\r
2  $Id: random.c,v $\r
3 \r
4  This program is public domain and was written by William S. England\r
5  (Oct 1988).  It is based on an article by:\r
6 \r
7  Stephen K. Park and Keith W. Miller. RANDOM NUMBER GENERATORS:\r
8  GOOD ONES ARE HARD TO FIND. Communications of the ACM,\r
9  New York, NY.,October 1988 p.1192\r
10 \r
11  Modifications;\r
12 \r
13  $Log: random.c,v $\r
14 \r
15 ######\r
16 \r
17  The following is a portable c program for generating random numbers.\r
18  The modulus and multiplier have been extensively tested and should\r
19  not be changed except by someone who is a professional Lehmer generator\r
20  writer.  THIS GENERATOR REPRESENTS THE MINIMUM STANDARD AGAINST WHICH\r
21  OTHER GENERATORS SHOULD BE JUDGED. ("Quote from the referenced article's\r
22  authors. WSE" )\r
23 */\r
24 \r
25 /*\r
26 **  These are pre-calculated  below to compensate for c \r
27 **  compilers that may overflow when building the code.\r
28 **\r
29 **  q = (m / a)\r
30 **  r = (m % a)\r
31 */\r
32 \r
33 /*\r
34 ** To build the generator with the original ACM\r
35 ** article's numbers use -DORIGINAL_NUMBERS when compiling.\r
36 **\r
37 ** Original_numbers are the original published m and q in the\r
38 ** ACM article above.  John Burton has furnished numbers for\r
39 ** a reportedly better generator.  The new numbers are now\r
40 ** used in this program by default.\r
41 */\r
42 \r
43 #ifndef ORIGINAL_NUMBERS\r
44 #define m  (unsigned long)2147483647\r
45 #define q  (unsigned long)44488\r
46 \r
47 #define a (unsigned int)48271\r
48 #define r (unsigned int)3399\r
49 \r
50 #define successfulltest 399268537\r
51 #endif\r
52 \r
53 #ifdef ORIGINAL_NUMBERS\r
54 #define m  (unsigned long)2147483647\r
55 #define q  (unsigned long)127773\r
56 \r
57 #define a (unsigned int)16807\r
58 #define r (unsigned int)2836\r
59 \r
60 #define successfulltest 1043618065\r
61 #endif\r
62 \r
63 /*\r
64 ** F(z) = (az)%m\r
65 **      = az-m(az/m)\r
66 **\r
67 ** F(z)  = G(z)+mT(z)\r
68 ** G(z)  = a(z%q)- r(z/q)\r
69 ** T(z)  = (z/q) - (az/m)\r
70 **\r
71 ** F(z)  = a(z%q)- rz/q+ m((z/q) - a(z/m))\r
72 **       = a(z%q)- rz/q+ m(z/q) - az\r
73 */\r
74 \r
75 unsigned long seed;\r
76 \r
77 void srand( /* unsigned long*/ initial_seed)\r
78 unsigned long initial_seed;\r
79 {\r
80     seed = initial_seed;\r
81     return;\r
82 }\r
83 /*\r
84 **\r
85 */\r
86 unsigned long rand(/*void*/){\r
87 \r
88 register\r
89 int     lo, hi, test;\r
90 \r
91     hi   = seed/q;\r
92     lo   = seed%q;\r
93 \r
94     test = a*lo - r*hi;\r
95 \r
96     if (test > 0)\r
97         seed = test;\r
98     else\r
99         seed = test+ m;\r
100 \r
101     return seed;\r
102 }\r
103 \r
104 #ifdef TESTRAND\r
105 #include <stdio.h> \r
106 /*  \r
107 **   The result of running this program should be\r
108 **   "successfulltest".  If this program does not yield this\r
109 **   value then your compiler has not implemented this\r
110 **   program correctly.\r
111 **   \r
112 **   To compile with test option under unix use; 'cc -DTESTRAND random.c'\r
113 **\r
114 **   Be sure to compile without test option for use in applications.\r
115 **   ( Now why did I have to say that ??? )\r
116 */\r
117 \r
118 main(/*void*/)\r
119 {\r
120 unsigned \r
121 long    n_rand;\r
122 \r
123 register int    i;\r
124 int     success = 0;\r
125 \r
126     srand(1);\r
127 \r
128     for( i = 1; i <= 10001; i++){\r
129         n_rand = rand();\r
130 \r
131         if( i> 9998)  \r
132             printf("Sequence %5i, Seed= %10i\n", i, seed ); \r
133 \r
134         if( i == 10000) \r
135             if( seed == successfulltest ) \r
136                 success = 1;\r
137     }\r
138 \r
139     if (success){\r
140         printf("The random number generator works correctly.\n\n");\r
141         exit(0);\r
142     }else{\r
143         printf("The random number generator DOES NOT WORK!\n\n");\r
144         exit(1);\r
145     }\r
146 }\r
147 #endif\r