source: git/factory/cf_random.cc @ 9f7665

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