コード例 #1
0
ファイル: userintf.cpp プロジェクト: FelixMay/confettiRbasic
void FatalError(const char * ErrorText) {
  // This function outputs an error message and aborts the program.

  // Important: There is no universally portable way of outputting an 
  // error message. You may have to modify this function to output
  // the error message in a way that is appropriate for your system.


  // Check if FatalAppExit exists (this macro is defined in winbase.h)
  #ifdef FatalAppExit  

  // in Windows, use FatalAppExit:
  FatalAppExit(0, ErrorText);

  #else

  // in console mode, print error message  
  printf ("\n%s\n", ErrorText);
  EndOfProgram();

  #endif

  // Terminate program with error code
  exit(1);
}
コード例 #2
0
int main() {
   int seed = (int)time(0);            // random seed

   // choose one of the random number generators:
   CRandomMersenne RanGen(seed);       // make instance of random number generator

   int i;                              // loop counter
   int ir;                             // random integer number
   double fr;                          // random floating point number


   // make random integers in interval from 0 to 99, inclusive:
   printf("\n\nRandom integers in interval from 0 to 99:\n");
   for (i = 0; i < 40; i++) {
      ir = RanGen.IRandom(0,99);
      printf ("%6i  ", ir);
   }

   // make random floating point numbers in interval from 0 to 1:
   printf("\n\nRandom floating point numbers in interval from 0 to 1:\n");
   for (i = 0; i < 32; i++) {
      fr = RanGen.Random();
      printf ("%8.6f  ", fr);
   }

   // make random bits
   printf("\n\nRandom bits (hexadecimal):\n");
   for (i = 0; i < 32; i++) {
      ir = RanGen.BRandom();
      printf ("%08lX  ", ir);
   }

   EndOfProgram();                     // system-specific exit code
   return 0;
}
コード例 #3
0
ファイル: ex-lotto.cpp プロジェクト: CISMM/video
int main () {
  // define constants
  const int nmin = 1;                  // set the lowest allowed number
  const int nmax = 36;                 // set the highest allowed number
  const int select = 6;                // the number of numbers to draw  

  int32 seed = time(0);                // generate random seed
  StochasticLib1 sto(seed);            // make instance of random library
  int urn[nmax-nmin+1];                // contains the shuffled numbers
  int i;                               // loop counter

  // Make shuffled list of numbers from nmin to nmax:
  sto.Shuffle(urn, nmin, nmax-nmin+1);

  // output heading text
  printf("Random numbers without duplicates:\n\n");
  
  // loop to output the first "select" numbers
  for (i=0; i < select; i++) {
    if (i < nmax-nmin+1) {
      // output number
      printf("%4i    ", urn[i]);}}

     
  EndOfProgram();                      // system-specific exit code
  return 0;}
コード例 #4
0
ファイル: rancombi.cpp プロジェクト: plamkata/TestProjects
int main() {
   // Make an object of the template class. The names inside <> define the
   // class names of the two random number generators to combine.
   // Use time as seed.
   int seed = (int)time(0);
   int i;
   // Declare object of combined generator
   TRandomCombined<CRandomMersenne,CRandomMother> RG(seed);

   for (i = 0; i < 20; i++) {
      // generate 20 random floating point numbers and 20 random integers
      printf("\n%14.10f   %2i",  RG.Random(),  RG.IRandom(0,99));
   }

   printf("\n");
   EndOfProgram();
   return 0;
}
コード例 #5
0
ファイル: ex-ran.cpp プロジェクト: danvk/performance-boggle
int main() {
    int32 seed = time(0);                // random seed

    // choose one of the random number generators:
    TRandomMersenne rg(seed);            // make instance of random number generator
    // or:
    // TRanrotWGenerator rg(seed);       // make instance of random number generator
    // or:
    // TRandomMotherOfAll rg(seed);      // make instance of random number generator

    int i;                               // loop counter
    double r;                            // random number
    int32 ir;                            // random integer number


    // make random integers in interval from 0 to 99, inclusive:
    printf("\n\nRandom integers in interval from 0 to 99:\n");
    for (i=0; i<40; i++) {
        ir = rg.IRandom(0,99);
        printf ("%6li  ", ir);
    }

    // make random floating point numbers in interval from 0 to 1:
    printf("\n\nRandom floating point numbers in interval from 0 to 1:\n");
    for (i=0; i<32; i++) {
        r = rg.Random();
        printf ("%8.6f  ", r);
    }

    // make random bits (Not for TRandomMotherOfAll):
    printf("\n\nRandom bits (hexadecimal):\n");
    for (i=0; i<32; i++) {
        ir = rg.BRandom();
        printf ("%08lX  ", ir);
    }

    EndOfProgram();                      // system-specific exit code
    return 0;
}
コード例 #6
0
ファイル: ex-cards.cpp プロジェクト: bnichols1979/Simera
int main () {
   int seed = (int)time(0);            // generate random seed
   StochasticLib1 sto(seed);           // make instance of random library
   int deck[52];                       // deck of 52 cards
   const char * ColorNames[] = {       // names of 4 colors
      "clubs", "diamonds", "hearts", "spades"
   };
   const char * ValueNames[] = {       // names of 13 card values
      "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
   };
   int i;                              // loop counter
   int color;                          // card color
   int value;                          // card value

   // Make shuffled list of cards. 
   // The 52 cards are numbered from 0 to 51, where 0 = A-clubs, 1 = A-diamonds,
   // 51 = K-spades:
   sto.Shuffle(deck, 0, 52);

   // output heading text
   printf("Shuffled deck of cards:\n\n");

   // loop for all cards
   for (i = 0; i < 52; i++) {
      // translate card number into color and value
      color = deck[i] % 4;
      value = deck[i] / 4;

      // output card
      printf("%8s %2s     ", ColorNames[color], ValueNames[value]);
      // make linefeed for every four cards
      if (i % 4 == 3) printf("\n");
   }

   EndOfProgram();                     // system-specific exit code
   return 0;
}
コード例 #7
0
ファイル: testbino.cpp プロジェクト: CISMM/video
// main  
int main() {

  // parameters. You may change these
  int32 n = 10;                        // number of balls
  double p = 0.4;                      // probability
  int32 nn = 1000000;                  // number of samples
  
  // other variables
  double sum;                          // sum
  double ssum;                         // squaresum
  int32 min, max;                      // minimum, maximum
  double mean;                         // mean
  double var;                          // variance
  int32 i;                             // loop counter
  int32 x;                             // random variate
  const int DSIZE = 18;                // size of array
  int32 dis[DSIZE] = {0};              // number of samples in each range
  int c;                               // index into dis list
  double f;                            // calculated function value
  double xme;                          // expected mean
  double xva;                          // expected variance
  double xsd;                          // expected standard deviation
  int32 delta;                         // step in list
  int32 xa;                            // list minimum
  int32 x1, x2;                        // category range
  
  // make random library
  int32 seed = time(0);                // random seed
  StochasticLib1 sto(seed);            // make instance of random library

  // calculate mean and variance
  xme = n*p;                           // calculated mean
  xva = n*p*(1-p);                     // calculated variance

  // calculate appropriate list divisions
  xsd = sqrt(xva);                     // calculated std.dev.
  xa = int(xme - 6.*xsd + 0.5);        // calculated minimum
  if (xa < 0) xa=0;
  delta = int(12.*xsd/(DSIZE-1));      // list step
  if (delta < 1) delta = 1; 

  // initialize
  sum = ssum = 0; min = 2000000000; max = -1;

  // start message
  printf("taking %li samples from binomial distribution...", nn);
  
  // sample nn times
  for (i=0; i < nn; i++) {

    // generate random number with desired distribution
    x = sto.Binomial(n,p);

    // update sums
    sum += x;
    ssum += (double)x*x;
    if (x < min) min = x;
    if (x > max) max = x;
    c = (x-xa)/delta;
    if (c < 0) c = 0;
    if (c >= DSIZE) c = DSIZE-1;
    dis[c]++;}
    
  // calculate sampled mean and variance
  mean = sum / nn; 
  var = (ssum - sum*sum/nn) / nn;

  // print sampled and theoretical mean and variance
  printf("\n\nparameters: n=%li, p=%.3G", n, p);
  printf("\n                mean        variance");
  printf("\nsampled:   %12.6f %12.6f", mean, var);
  printf("\nexpected:  %12.6f %12.6f", xme,  xva);

  // print found and expected frequencies
  printf("\n\n      x              found     expected");
  for (c = 0; c < DSIZE; c++) {
    x1 = c*delta + xa;
    if (x1 > n) break;
    x2 = x1+delta-1;
    if (c==0 && min<x1) x1 = min;
    if (c==DSIZE-1 && max>x2) x2 = max;
    if (x2 > n) x2 = n;

    // calculate expected frequency
    for (x=x1, f=0.; x <= x2; x++) {
      f += exp(LnFac(n) - LnFac(x) - LnFac(n-x) + x*log(p) + (n-x)*log(1-p));}

    // output found and expected frequency
    if (dis[c] || f*nn > 1E-4) {
      if (x1==x2) {
        printf("\n%7li       %12.6f %12.6f", x1, (double)dis[c]/nn, f);}
      else {
        printf("\n%6li-%-6li %12.6f %12.6f", x1, x2, (double)dis[c]/nn, f);}}}
    
  EndOfProgram();                      // system-specific exit code
  return 0;}
コード例 #8
0
ファイル: testwnch.cpp プロジェクト: bnichols1979/Simera
// main  
int main() {
   // hypergeometric parameters. You may change these
   int32_t n =  600;                   // number of balls taken
   int32_t m =  400;                   // number of red balls
   int32_t N = 1000;                   // total number of balls
   double omega = 3;                   // odds 
   int32_t nn = 100000;                // number of samples

   // other variables
   double sum;                         // sum
   double ssum;                        // squaresum
   int32_t min, max;                   // minimum, maximum
   double mean;                        // mean
   double var;                         // variance
   int32_t i;                          // loop counter
   int32_t x;                          // random variable
   const int DSIZE = 18;               // size of array
   int32_t dis[DSIZE] = {0};           // number of samples in each range
   int c;                              // index into dis list
   double f;                           // calculated function value
   double xme1;                        // expected mean (approximate)
   double xme2;                        // expected mean (exact)
   double xva;                         // expected variance
   double xsd;                         // expected standard deviation
   int32_t delta;                      // step size in list
   int32_t xa, xb;                     // list minimum, maximum
   int32_t x1, x2;                     // category range

   // make random seed
   int32_t seed = (int32_t)time(0);    // random seed = time

   // make instance of random library for producing random variates
   StochasticLib3 sto(seed);

   // make object for calculating mean, variance and probabilities
   CWalleniusNCHypergeometric nchyp(n, m, N, omega, 1E-10f);

   // calculate approximate mean
   xme1 = nchyp.mean();

   // calculate exact mean and variance, and check precision
   sum = nchyp.moments(&xme2, &xva);
   // printf("obtained precision %.4G\n", sum-1.);

   // calculate appropriate list divisions
   xsd = sqrt(xva);                    // std.deviation
   xa = int(xme2 - 6.*xsd + 0.5);      // expected minimum
   if (xa < 0) xa=0;
   if (xa < n+m-N) xa = n+m-N;  
   delta = int(12.*xsd/(DSIZE-1));     // list step
   if (delta < 1) delta=1; 
   xb = xa + (DSIZE-1)*delta;          // expected maximum
   if (xb > n) xb = n;
   if (xb > m) xb = m;

   // initialize
   sum = ssum = 0; min = 2000000000; max = -1;

   // start message
   printf("taking %li samples from Wallenius noncentral hypergeometric distribution...", nn);

   // sample nn times
   for (i = 0; i < nn; i++) {

      // generate random number with desired distribution
      x = sto.WalleniusNCHyp(n, m, N, omega);

      // update sums
      sum += x;
      ssum += (double)x*x;
      if (x < min) min = x;
      if (x > max) max = x;
      c = (x-xa)/delta;
      if (c < 0) c = 0;
      if (c >= DSIZE) c = DSIZE-1;
      dis[c]++;
   }

   // calculate sampled mean and variance
   mean = sum / nn; 
   var = (ssum - sum*sum/nn) / nn;

   // print sampled and theoretical mean and variance
   printf("\n\nparameters: n=%li, m=%li, N=%li, odds=%.3G", n, m, N, omega);
   printf("\n                mean        variance");
   printf("\nsampled:   %12.6f %12.6f", mean, var);
   printf("\nexpected:  %12.6f %12.6f", xme2, xva);

   // print found and expected frequencies
   printf("\n\n      x              found     expected");
   for (c = 0; c < DSIZE; c++) {
      x1 = c*delta + xa;
      if (x1 > xb) break;
      x2 = x1+delta-1;
      if (x2 > xb) x2 = xb;
      if (c==0 && min<x1) x1 = min;
      if (c==DSIZE-1 && max>x2) x2 = max;

      // calculate expected frequency
      for (x=x1, f=0.; x <= x2; x++) {
         f += nchyp.probability(x);
      }
      if (dis[c] || f*nn > 1E-4) {
         if (x1==x2) {
            printf("\n%7li       %12.6f %12.6f", x1, (double)dis[c]/nn, f);
         }
         else {
            printf("\n%6li-%-6li %12.6f %12.6f", x1, x2, (double)dis[c]/nn, f);
         }
      }
   }

   EndOfProgram();                     // system-specific exit code
   return 0;
}