Changeset 4345a0 in git for factory/cf_random.cc


Ignore:
Timestamp:
Apr 18, 1997, 3:35:59 PM (27 years ago)
Author:
Jens Schmidt <schmidt@…>
Branches:
(u'spielwiese', '4a9821a93ffdc22a6696668bd4f6b8c9de3e6c5f')
Children:
5ec6957bea2ca39b00d7f174d456bba93f41ea83
Parents:
7facdcf4421e259e6dd55850b1c5b97b84abaa90
Message:
o new class RandomGenerator which replaces calls to the
  stdlib functions rand90, srand() in factoryrandom() and
  factoryseed()


git-svn-id: file:///usr/local/Singular/svn/trunk@176 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • factory/cf_random.cc

    r7facdcf r4345a0  
    11// emacs edit mode for this file is -*- C++ -*-
    2 // $Id: cf_random.cc,v 1.1 1997-04-07 16:10:17 schmidt Exp $
     2// $Id: cf_random.cc,v 1.2 1997-04-18 13:35:59 schmidt Exp $
    33
    44/*
    55$Log: not supported by cvs2svn $
     6Revision 1.1  1997/04/07 16:10:17  schmidt
     7#include <config.h> added
     8
    69Revision 1.0  1996/05/17 10:59:44  stobbe
    710Initial revision
     
    1114#include <config.h>
    1215
    13 #include <stdlib.h>
    14 #include <math.h>
     16#include <time.h>
    1517
    1618#include "assert.h"
     
    2224#include "imm.h"
    2325
     26class RandomGenerator {
     27private:
     28    const long int
     29        ia = 16807,
     30        im = 2147483647,
     31        iq = 127773,
     32        ir = 2836,
     33        deflt = 123459876;
     34   
     35    long s;
     36
     37    // s must not equal zero!
     38    void seedInit( long ss ) { s = ((ss == 0) ? deflt : ss); }
     39public:
     40    RandomGenerator() { seedInit( (long)time( 0 ) ); }
     41    RandomGenerator( long ss ) { seedInit( ss ); }
     42    ~RandomGenerator() {}
     43    long generate();
     44    void seed( long ss ) { seedInit( ss ); }
     45};
     46
     47long
     48RandomGenerator::generate()
     49{
     50    long k;
     51
     52    k = s/iq;
     53    s = ia*(s-k*iq)-ir*k;
     54    if ( s < 0 ) s += im;
     55
     56    return s;
     57}
     58
     59RandomGenerator ranGen;
    2460
    2561CanonicalForm FFRandom::generate () const
     
    135171int factoryrandom( int n )
    136172{
    137     return rand() % n;
     173    return ranGen.generate() % n;
    138174}
    139175
    140176void factoryseed ( int s )
    141177{
    142     srand( s );
     178    ranGen.seed( s );
    143179}
Note: See TracChangeset for help on using the changeset viewer.