source: git/factory/cf_random.cc @ f78374

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