source: git/factory/cf_random.cc @ 0dff6bc

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