\section{Rand.cpp} <>= // File: Rand.ccp // Last Revision: 23-Mar-2001 // Description: functions for the generation of random values. // programmer: Romeo Rizzi. // rrizzi@rtm.science.unitn.it // // responsible: Roberto Battiti. // battiti@science.unitn.it // // group: LEA (Laboratory for Experimental Algorithmics). // http://rtm.science.unitn.it/ // // Note: The two functions myrand() and smyrand() were written by Dell'Amico // using the portable pseudorandom generator RnG (see below). #include "Rand.H" #define PRANDMAX 1000000000 int dmxa, dmxb, dmxarr[55]; float myrand() { return ((float)lprand()) / PRANDMAX; } float smyrand(int seed) { return (float) sprand(seed); } int dado(int num_facce) { return (int) (num_facce * myrand()); } /* This file contains a set of c-language functions for generating uniform integers. This is a COMPLETELY PORTABLE generator. it will give iDEnTiCAL sequences of random numbers for any architecture with at least 30-bit integers, regardless of the integer representation, MAXinT value, or roundoff/truncation method, etc. This Truly Remarkable RnG is described more fully in j. Bentley's column, ``The Software Exploratorium '' to appear in Unix Review in 1991. it is based on one in knuth, Vol 2, Section 3.2.2 (Algorithm A) This is a little driver program so you can test the code. Giving input parameters: 0 3 1 should produce 921674862 250065336 377506581 Giving 1000000 1 2 should produce 572653995 void main() { int i,j,n,m,seed; scanf("%d %d %d",&m,&n,&seed); i = sprand(seed); for(i=0; i< m;i++) j = lprand(); for (i=0; i< n; i++) printf("\n%ld",lprand()); } */ // ---------RnG--------------------- // Returns long ints from the // range 0...PRANDMAX-1 int lprand() { dmxa --; if (dmxa < 0) dmxa = 54; dmxb --; if (dmxb < 0) dmxb = 54; int t = dmxarr[dmxa] - dmxarr[dmxb]; return dmxarr[dmxa] = ( (t < 0) ? t + PRANDMAX : t); } //-----------RnG initializer------------ // Call once before using lprand int sprand (int seed) { int i, ii, last, next; dmxarr[0] = last = seed; next = 1; for (i=1; i< 55; i++ ) { ii = 21 * i % 55; dmxarr[ii] = next; next = last - next; if (next < 0) next += PRANDMAX; last = dmxarr[ii]; } dmxa = 0; dmxb = 24; for(i = 0; i<= 164; i++) lprand(); return seed; } @