source: git/factory/cf_random.cc @ 553daf

spielwiese
Last change on this file since 553daf was 553daf, checked in by Jens Schmidt <schmidt@…>, 27 years ago
* cf_random.cc (RandomGenerator): removed definitions of const long int in class for mac. Instead, they are initialized in the constructor. git-svn-id: file:///usr/local/Singular/svn/trunk@470 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.5 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_random.cc,v 1.4 1997-07-01 12:35:11 schmidt Exp $ */
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
16class RandomGenerator {
17private:
18#ifdef macintosh
19    // mac does not like const long int in a class
20    long int ia, im, iq, ir, deflt;
21#else
22    const long int
23        ia = 16807,
24        im = 2147483647,
25        iq = 127773,
26        ir = 2836,
27        deflt = 123459876;
28#endif
29   
30    long s;
31
32    // s must not equal zero!
33    void seedInit( long ss ) { s = ((ss == 0) ? deflt : ss); }
34public:
35#ifdef macintosh
36    RandomGenerator();
37    RandomGenerator( long ss );
38#else
39    RandomGenerator() { seedInit( (long)time( 0 ) ); }
40    RandomGenerator( long ss ) { seedInit( ss ); }
41#endif
42    ~RandomGenerator() {}
43    long generate();
44    void seed( long ss ) { seedInit( ss ); }
45};
46
47#ifdef macintosh
48RandomGenerator::RandomGenerator()
49{
50  ia = 16807;
51  im = 2147483647;
52  iq = 127773;
53  ir = 2836;
54  deflt = 123459876;
55  seedInit( (long)time( 0 ) );
56}
57
58RandomGenerator::RandomGenerator( long ss )
59{
60  ia = 16807;
61  im = 2147483647;
62  iq = 127773;
63  ir = 2836;
64  deflt = 123459876;
65  seedInit( ss );
66}
67#endif
68
69long
70RandomGenerator::generate()
71{
72    long k;
73
74    k = s/iq;
75    s = ia*(s-k*iq)-ir*k;
76    if ( s < 0 ) s += im;
77
78    return s;
79}
80
81RandomGenerator ranGen;
82
83CanonicalForm FFRandom::generate () const
84{
85    return CanonicalForm( int2imm_p( factoryrandom( ff_prime ) ) );
86}
87
88CFRandom * FFRandom::clone () const
89{
90    return new FFRandom();
91}
92
93CanonicalForm GFRandom::generate () const
94{
95    return CanonicalForm( int2imm_gf( factoryrandom( gf_q ) ) );
96}
97
98CFRandom * GFRandom::clone () const
99{
100    return new GFRandom();
101}
102
103
104IntRandom::IntRandom()
105{
106    max = 100;
107}
108
109IntRandom::IntRandom( int m )
110{
111    max = m;
112}
113
114IntRandom::~IntRandom() {}
115
116CanonicalForm IntRandom::generate() const
117{
118    return factoryrandom( max );
119}
120
121CFRandom * IntRandom::clone() const
122{
123    return new IntRandom( max );
124}
125
126AlgExtRandomF::AlgExtRandomF()
127{
128    ASSERT( 0, "not a valid random generator" );
129}
130
131AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & )
132{
133    ASSERT( 0, "not a valid random generator" );
134}
135
136AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & )
137{
138    ASSERT( 0, "not a valid random generator" );
139    return *this;
140}
141
142AlgExtRandomF::AlgExtRandomF( const Variable & v )
143{
144    ASSERT( v.level() < 0, "not an algebraic extension" );
145    algext = v;
146    n = degree( getMipo( v ) );
147    gen = CFRandomFactory::generate();
148}
149
150AlgExtRandomF::AlgExtRandomF( const Variable & v1, const Variable & v2 )
151{
152    ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
153    algext = v2;
154    n = degree( getMipo( v2 ) );
155    gen = new AlgExtRandomF( v1 );
156}
157
158AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn )
159{
160    algext = v;
161    n = nn;
162    gen = g;
163}
164
165AlgExtRandomF::~AlgExtRandomF()
166{
167    delete gen;
168}
169
170CanonicalForm AlgExtRandomF::generate() const
171{
172    CanonicalForm result;
173    for ( int i = 0; i < n; i++ )
174        result += power( algext, i ) * gen->generate();
175    return result;
176};
177
178CFRandom * AlgExtRandomF::clone () const
179{
180    return new AlgExtRandomF( algext, gen->clone(), n );
181}
182
183CFRandom * CFRandomFactory::generate()
184{
185    if ( getCharacteristic() == 0 )
186        return new IntRandom();
187    if ( getGFDegree() > 1 )
188        return new GFRandom();
189    else
190        return new FFRandom();
191}
192
193int factoryrandom( int n )
194{
195    return ranGen.generate() % n;
196}
197
198void factoryseed ( int s )
199{
200    ranGen.seed( s );
201}
Note: See TracBrowser for help on using the repository browser.