Blame tests/Auto/lib/Synth/other/Random.c

Packit b099d7
/* 
Packit b099d7
 * Motif
Packit b099d7
 *
Packit b099d7
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
Packit b099d7
 *
Packit b099d7
 * These libraries and programs are free software; you can
Packit b099d7
 * redistribute them and/or modify them under the terms of the GNU
Packit b099d7
 * Lesser General Public License as published by the Free Software
Packit b099d7
 * Foundation; either version 2 of the License, or (at your option)
Packit b099d7
 * any later version.
Packit b099d7
 *
Packit b099d7
 * These libraries and programs are distributed in the hope that
Packit b099d7
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
Packit b099d7
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Packit b099d7
 * PURPOSE. See the GNU Lesser General Public License for more
Packit b099d7
 * details.
Packit b099d7
 *
Packit b099d7
 * You should have received a copy of the GNU Lesser General Public
Packit b099d7
 * License along with these librararies and programs; if not, write
Packit b099d7
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Packit b099d7
 * Floor, Boston, MA 02110-1301 USA
Packit b099d7
*/ 
Packit b099d7
/* 
Packit b099d7
 * HISTORY
Packit b099d7
*/ 
Packit b099d7
#ifdef REV_INFO
Packit b099d7
#ifndef lint
Packit b099d7
static char rcsid[] = "$XConsortium: Random.c /main/7 1995/07/14 11:36:33 drk $"
Packit b099d7
#endif
Packit b099d7
#endif
Packit b099d7
/***********************************************************************
Packit b099d7
  	@(#)Random.c	1.3.1.1	Date:1/22/91
Packit b099d7
  	Author: TAT
Packit b099d7
	History:
Packit b099d7
            05/24/90 SJS add to sccs
Packit b099d7
	Calls:
Packit b099d7
Packit b099d7
	Summary:
Packit b099d7
            This file contains routines that manipulate RANDOM NUMBERS
Packit b099d7
 
Packit b099d7
************************************************************************/
Packit b099d7
#include "xislib.h"
Packit b099d7
#include <math.h>
Packit b099d7
Packit b099d7
#define MAX_ERROR 1e-8
Packit b099d7
#define INV_MAX_ERROR 1e8
Packit b099d7
#define ROUND(x) ( floor((double)(x)*INV_MAX_ERROR + 0.5) * MAX_ERROR )
Packit b099d7
Packit b099d7
Packit b099d7
#define RANDOM_TABLE_SIZE 97
Packit b099d7
Packit b099d7
/*************************************************************
Packit b099d7
*           RANDOM INTEGER FROM 0.0 to 1.0                   *
Packit b099d7
**************************************************************
Packit b099d7
*
Packit b099d7
*       Returns a uniform random deviate between 0.0 and 1.0. 
Packit b099d7
*       Set seed to any negative value to reinitialize sequence.
Packit b099d7
*
Packit b099d7
**************************************************************/
Packit b099d7
Packit b099d7
static double xisPortableRandom(seed)           
Packit b099d7
long seed; 
Packit b099d7
{
Packit b099d7
    static first_pass = 1;
Packit b099d7
    static long m1=259200, ia1=7141, ic1=54773;
Packit b099d7
    static long m2=134456, ia2=8121, ic2=28411;
Packit b099d7
    static long m3=243000, ia3=4561, ic3=51349;
Packit b099d7
    static long glix1,glix2,glix3;
Packit b099d7
    static double glr[RANDOM_TABLE_SIZE];
Packit b099d7
    static double rm1,rm2;
Packit b099d7
    long j;
Packit b099d7
    double return_value;
Packit b099d7
Packit b099d7
    /** initialize table and glix's on first call **/
Packit b099d7
Packit b099d7
    if ((seed < 0)||first_pass) {  
Packit b099d7
        first_pass = 0;
Packit b099d7
        rm1 = (double)1.0/m1;
Packit b099d7
        rm2 = (double)1.0/m2;
Packit b099d7
        glix1 = (ic1 - seed) % m1;   /* Seed the first routine */                                               
Packit b099d7
        glix1 = (ia1*glix1 + ic1) % m1;
Packit b099d7
        glix2 = glix1 % m2;        /* and use it to seed the second */
Packit b099d7
        glix1 = (ia1*glix1 + ic1) % m1;
Packit b099d7
        glix3 = glix1 % m3;          /* and third routines. */
Packit b099d7
Packit b099d7
        /* Fill the table with sequential uniform deviates generated by the */
Packit b099d7
        /* first two routines.                                              */
Packit b099d7
        for (j=0; j < RANDOM_TABLE_SIZE; j++) {   
Packit b099d7
            glix1 = (ia1*glix1 + ic1) % m1;
Packit b099d7
            glix2 = (ia2*glix2 + ic2) % m2;
Packit b099d7
            glr[j] = (glix1 + glix2*rm2)*rm1; /* Combine low,high order pieces*/
Packit b099d7
        }
Packit b099d7
    }
Packit b099d7
Packit b099d7
    /*** Except when initializing, this is where we start. ****/
Packit b099d7
Packit b099d7
    /* Generate the next number for each sequence. */
Packit b099d7
Packit b099d7
    glix1 = (ia1*glix1 + ic1) % m1;  
Packit b099d7
    glix2 = (ia2*glix2 + ic2) % m2;  
Packit b099d7
    glix3 = (ia3*glix3 + ic3) % m3;  
Packit b099d7
Packit b099d7
    /* Use the third sequence to get an integer */
Packit b099d7
Packit b099d7
    j = (RANDOM_TABLE_SIZE*glix3) / m3;   
Packit b099d7
Packit b099d7
    if ((j >= RANDOM_TABLE_SIZE) || (j < 0))
Packit b099d7
        (*xisWarningMsg)("in xisPortableRandom() j=%d is out of range\n",j);
Packit b099d7
                                         
Packit b099d7
    return_value = glr[j];                   /* Return that table entry and */
Packit b099d7
    glr[j] = ROUND((glix1 + glix2*rm2)*rm1); /* fill it with a new table entry*/
Packit b099d7
Packit b099d7
    return (return_value);
Packit b099d7
}
Packit b099d7

Packit b099d7
/*************************************************************
Packit b099d7
*           INITIALIZE RANDOM
Packit b099d7
**************************************************************                         
Packit b099d7
*
Packit b099d7
*  Initializes the psuedo random number generator.
Packit b099d7
*
Packit b099d7
**************************************************************/
Packit b099d7
void xisInitRandom(num)
Packit b099d7
int num;
Packit b099d7
{
Packit b099d7
    if (num > 0)
Packit b099d7
        num = (-num);
Packit b099d7
    else if (num == 0)
Packit b099d7
        num = -5678;
Packit b099d7
    xisPortableRandom((long)(num));
Packit b099d7
}
Packit b099d7

Packit b099d7
/*************************************************************
Packit b099d7
*           SELECT AN ARBITRARY INTEGER
Packit b099d7
**************************************************************                         
Packit b099d7
*
Packit b099d7
*  Selects an arbitrary integer between lo and hi.
Packit b099d7
*
Packit b099d7
**************************************************************/
Packit b099d7
int xisArbitrary(lo,hi)
Packit b099d7
int lo,hi;
Packit b099d7
{
Packit b099d7
    long i;
Packit b099d7
    i = ROUND(((hi+0.9999)-lo)*xisPortableRandom(1) + lo);
Packit b099d7
    return(i);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7