source: git/factory/cf_random.cc @ 37a11e

fieker-DuValspielwiese
Last change on this file since 37a11e 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
RevLine 
[493c477]1/* emacs edit mode for this file is -*- C++ -*- */
[2dd068]2
[9f7665]3
[e4fe2b]4#include "config.h"
[9f7665]5
[b973c0]6
[4345a0]7#include <time.h>
[2dd068]8
[650f2d8]9#include "cf_assert.h"
[b973c0]10
[2dd068]11#include "cf_defs.h"
12#include "cf_random.h"
13#include "ffops.h"
14#include "gfops.h"
15#include "imm.h"
16
[4345a0]17class RandomGenerator {
18private:
[f67df3]19    const int ia, im, iq, ir, deflt;
20    int s;
[4345a0]21
22    // s must not equal zero!
[f67df3]23    void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
[4345a0]24public:
[553daf]25    RandomGenerator();
[f67df3]26    RandomGenerator( int ss );
[4345a0]27    ~RandomGenerator() {}
[f67df3]28    int generate();
29    void seed( int ss ) { seedInit( ss ); }
[4345a0]30};
31
[9c9e2a4]32RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
[553daf]33{
[f67df3]34  seedInit( (int)time( 0 ) );
[553daf]35}
36
[f67df3]37RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
[553daf]38{
39  seedInit( ss );
40}
41
[f67df3]42int
[4345a0]43RandomGenerator::generate()
44{
[f67df3]45    int k;
[4345a0]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;
[2dd068]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{
[08daea]68    int i= factoryrandom( gf_q );
69    if ( i == gf_q1 ) i++;
70    return CanonicalForm( int2imm_gf( i ) );
[2dd068]71}
72
73CFRandom * GFRandom::clone () const
74{
75    return new GFRandom();
76}
77
78
79IntRandom::IntRandom()
80{
[afd067]81    max = 50;
[2dd068]82}
83
84IntRandom::IntRandom( int m )
85{
86    max = m;
87}
88
89IntRandom::~IntRandom() {}
90
91CanonicalForm IntRandom::generate() const
92{
[afd067]93    return factoryrandom( 2*max )-max;
[2dd068]94}
[b973c0]95
[2dd068]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++ )
[806c18]149      result += power( algext, i ) * gen->generate();
[2dd068]150    return result;
[e76d7a6]151}
[2dd068]152
153CFRandom * AlgExtRandomF::clone () const
154{
155    return new AlgExtRandomF( algext, gen->clone(), n );
156}
157
158CFRandom * CFRandomFactory::generate()
159{
160    if ( getCharacteristic() == 0 )
[806c18]161        return new IntRandom();
[2dd068]162    if ( getGFDegree() > 1 )
[806c18]163        return new GFRandom();
[2dd068]164    else
[806c18]165        return new FFRandom();
[2dd068]166}
167
168int factoryrandom( int n )
169{
[3aca31]170    if ( n == 0 )
[806c18]171        return (int)ranGen.generate();
[3aca31]172    else
[806c18]173        return ranGen.generate() % n;
[2dd068]174}
175
176void factoryseed ( int s )
177{
[4345a0]178    ranGen.seed( s );
[2dd068]179}
Note: See TracBrowser for help on using the repository browser.