NCBI C++ ToolKit
thrdrsmp.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: thrdrsmp.c 32839 2007-03-05 20:41:55Z kazimird $
2 *===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name: thrdrsmp.c
27 *
28 * Author: Stephen Bryant
29 *
30 * Initial Version Creation Date: 08/16/2000
31 *
32 * $Revision: 32839 $
33 *
34 * File Description: threader
35 */
36 
37 #ifdef _MSC_VER
38 #pragma warning(disable:4244) // disable double->float warning in MSVC
39 #endif
40 
41 /* Choose randomly from a multinomial distribution */
42 
45 #include <stdio.h>
46 #include <math.h>
47 
48 int rsmp(Rnd_Smp* pvl) {
49 /*----------------------------------------------------*/
50 /* pvl: Number and probabilities of parameter values */
51 /*----------------------------------------------------*/
52 
53 int i; /* The i-th offset parameter value will be the choice */
54 float c; /* Cumulative probabilites across parameter values */
55 float r; /* A uniform random number on the interval 0 - 1 */
56 int idum = 1;
57 
58 /* for(i=0;i<pvl->n;i++) printf("%.4f ",pvl->p[i]); printf("pvl->p\n"); */
59 
60 /* r=drand48(); c=0.; */
61 r = Rand01(&idum);
62 c = 0.0;
63 
64 /* printf("r: %.4f\n",r); */
65 
66 for(i=0; i<pvl->n; i++) {
67  c=c+pvl->p[i];
68  /* printf("c: %.4f\n",c); */
69  if(c>=r) return(i); }
70 
71 return(i-1);
72 }
73 
74 
75 #define M1 259200
76 #define IA1 7141
77 #define IC1 54773
78 #define RM1 (1.0/M1)
79 #define M2 134456
80 #define IA2 8121
81 #define IC2 28411
82 #define RM2 (1.0/M2)
83 #define M3 243000
84 #define IA3 4561
85 #define IC3 51349
86 
87 float Rand01(int* idum) {
88 /*----------------------------------------------------------------------------
89 // Returns a uniform deviate between 0.0 and 1.0.
90 // Set idum to any negative value to initialize or reinitialize the sequence.
91 //
92 // This is copied from Numerical Recipes, chapter 7.1, 1988.
93 // (ISBN 0-521-35465-X)
94 //--------------------------------------------------------------------------*/
95  static long ix1,ix2,ix3;
96  static float r[98];
97  float temp;
98  static int iff=0;
99  int j;
100 
101  if (*idum<0 || iff==0) {
102  iff=1;
103  ix1=(IC1-*idum) % M1;
104  ix1=(IA1*ix1+IC1) % M1;
105  ix2=ix1 % M2;
106  ix1=(IA1*ix1+IC1) % M1;
107  ix3=ix1 % M3;
108  for (j=1; j<=97; j++) {
109  ix1=(IA1*ix1+IC1) % M1;
110  ix2=(IA2*ix2+IC2) % M2;
111  r[j]=(ix1+ix2*RM2)*RM1;
112  }
113  *idum=1;
114  }
115  ix1=(IA1*ix1+IC1) % M1;
116  ix2=(IA2*ix2+IC2) % M2;
117  ix3=(IA3*ix3+IC3) % M3;
118  j=1 + ((97*ix3)/M3);
119  temp=r[j];
120  r[j]=(ix1+ix2*RM2)*RM1;
121  return(temp);
122 }
int i
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
float * p
Definition: thrdatd.h:308
int n
Definition: thrdatd.h:307
#define IC3
Definition: thrdrsmp.c:85
#define IC1
Definition: thrdrsmp.c:77
#define M2
Definition: thrdrsmp.c:79
#define IA2
Definition: thrdrsmp.c:80
#define RM2
Definition: thrdrsmp.c:82
#define RM1
Definition: thrdrsmp.c:78
#define IC2
Definition: thrdrsmp.c:81
float Rand01(int *idum)
Definition: thrdrsmp.c:87
#define IA1
Definition: thrdrsmp.c:76
#define IA3
Definition: thrdrsmp.c:84
#define M1
Definition: thrdrsmp.c:75
#define M3
Definition: thrdrsmp.c:83
int rsmp(Rnd_Smp *pvl)
Definition: thrdrsmp.c:48
Modified on Fri Sep 20 14:58:18 2024 by modify_doxy.py rev. 669887