1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | |
---|
6 | #include <time.h> |
---|
7 | |
---|
8 | #include "assert.h" |
---|
9 | |
---|
10 | #include "cf_defs.h" |
---|
11 | #include "cf_random.h" |
---|
12 | #include "ffops.h" |
---|
13 | #include "gfops.h" |
---|
14 | #include "imm.h" |
---|
15 | |
---|
16 | class RandomGenerator { |
---|
17 | private: |
---|
18 | const int ia, im, iq, ir, deflt; |
---|
19 | int s; |
---|
20 | |
---|
21 | // s must not equal zero! |
---|
22 | void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); } |
---|
23 | public: |
---|
24 | RandomGenerator(); |
---|
25 | RandomGenerator( int ss ); |
---|
26 | ~RandomGenerator() {} |
---|
27 | int generate(); |
---|
28 | void seed( int ss ) { seedInit( ss ); } |
---|
29 | }; |
---|
30 | |
---|
31 | RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876) |
---|
32 | { |
---|
33 | seedInit( (int)time( 0 ) ); |
---|
34 | } |
---|
35 | |
---|
36 | RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876) |
---|
37 | { |
---|
38 | seedInit( ss ); |
---|
39 | } |
---|
40 | |
---|
41 | int |
---|
42 | RandomGenerator::generate() |
---|
43 | { |
---|
44 | int k; |
---|
45 | |
---|
46 | k = s/iq; |
---|
47 | s = ia*(s-k*iq)-ir*k; |
---|
48 | if ( s < 0 ) s += im; |
---|
49 | |
---|
50 | return s; |
---|
51 | } |
---|
52 | |
---|
53 | RandomGenerator ranGen; |
---|
54 | |
---|
55 | CanonicalForm FFRandom::generate () const |
---|
56 | { |
---|
57 | return CanonicalForm( int2imm_p( factoryrandom( ff_prime ) ) ); |
---|
58 | } |
---|
59 | |
---|
60 | CFRandom * FFRandom::clone () const |
---|
61 | { |
---|
62 | return new FFRandom(); |
---|
63 | } |
---|
64 | |
---|
65 | CanonicalForm GFRandom::generate () const |
---|
66 | { |
---|
67 | return CanonicalForm( int2imm_gf( factoryrandom( gf_q ) ) ); |
---|
68 | } |
---|
69 | |
---|
70 | CFRandom * GFRandom::clone () const |
---|
71 | { |
---|
72 | return new GFRandom(); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | IntRandom::IntRandom() |
---|
77 | { |
---|
78 | max = 50; |
---|
79 | } |
---|
80 | |
---|
81 | IntRandom::IntRandom( int m ) |
---|
82 | { |
---|
83 | max = m; |
---|
84 | } |
---|
85 | |
---|
86 | IntRandom::~IntRandom() {} |
---|
87 | |
---|
88 | CanonicalForm IntRandom::generate() const |
---|
89 | { |
---|
90 | return factoryrandom( 2*max )-max; |
---|
91 | } |
---|
92 | |
---|
93 | CFRandom * IntRandom::clone() const |
---|
94 | { |
---|
95 | return new IntRandom( max ); |
---|
96 | } |
---|
97 | |
---|
98 | AlgExtRandomF::AlgExtRandomF() |
---|
99 | { |
---|
100 | ASSERT( 0, "not a valid random generator" ); |
---|
101 | } |
---|
102 | |
---|
103 | AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & ) |
---|
104 | { |
---|
105 | ASSERT( 0, "not a valid random generator" ); |
---|
106 | } |
---|
107 | |
---|
108 | AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & ) |
---|
109 | { |
---|
110 | ASSERT( 0, "not a valid random generator" ); |
---|
111 | return *this; |
---|
112 | } |
---|
113 | |
---|
114 | AlgExtRandomF::AlgExtRandomF( const Variable & v ) |
---|
115 | { |
---|
116 | ASSERT( v.level() < 0, "not an algebraic extension" ); |
---|
117 | algext = v; |
---|
118 | n = degree( getMipo( v ) ); |
---|
119 | gen = CFRandomFactory::generate(); |
---|
120 | } |
---|
121 | |
---|
122 | AlgExtRandomF::AlgExtRandomF( const Variable & v1, const Variable & v2 ) |
---|
123 | { |
---|
124 | ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" ); |
---|
125 | algext = v2; |
---|
126 | n = degree( getMipo( v2 ) ); |
---|
127 | gen = new AlgExtRandomF( v1 ); |
---|
128 | } |
---|
129 | |
---|
130 | AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn ) |
---|
131 | { |
---|
132 | algext = v; |
---|
133 | n = nn; |
---|
134 | gen = g; |
---|
135 | } |
---|
136 | |
---|
137 | AlgExtRandomF::~AlgExtRandomF() |
---|
138 | { |
---|
139 | delete gen; |
---|
140 | } |
---|
141 | |
---|
142 | CanonicalForm AlgExtRandomF::generate() const |
---|
143 | { |
---|
144 | CanonicalForm result; |
---|
145 | for ( int i = 0; i < n; i++ ) |
---|
146 | result += power( algext, i ) * gen->generate(); |
---|
147 | return result; |
---|
148 | }; |
---|
149 | |
---|
150 | CFRandom * AlgExtRandomF::clone () const |
---|
151 | { |
---|
152 | return new AlgExtRandomF( algext, gen->clone(), n ); |
---|
153 | } |
---|
154 | |
---|
155 | CFRandom * CFRandomFactory::generate() |
---|
156 | { |
---|
157 | if ( getCharacteristic() == 0 ) |
---|
158 | return new IntRandom(); |
---|
159 | if ( getGFDegree() > 1 ) |
---|
160 | return new GFRandom(); |
---|
161 | else |
---|
162 | return new FFRandom(); |
---|
163 | } |
---|
164 | |
---|
165 | int factoryrandom( int n ) |
---|
166 | { |
---|
167 | if ( n == 0 ) |
---|
168 | return (int)ranGen.generate(); |
---|
169 | else |
---|
170 | return ranGen.generate() % n; |
---|
171 | } |
---|
172 | |
---|
173 | void factoryseed ( int s ) |
---|
174 | { |
---|
175 | ranGen.seed( s ); |
---|
176 | } |
---|