File: StatUtils.cpp

package info (click to toggle)
pbseqlib 0~20161219-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,924 kB
  • ctags: 5,123
  • sloc: cpp: 82,727; makefile: 305; python: 239; sh: 8
file content (48 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "StatUtils.hpp" 
#ifndef __WORDSIZE
#  include <bits/user.h>
#endif

void InitializeRandomGenerator(int value) {
  srandom((unsigned) value);
}


unsigned int RandomUnsignedInt(unsigned int randMax) {
 // 
 //  step 1, pack an unsigned integer with a random value, this 
 //
 unsigned int randVal = RAND_MAX * (1.0*random())/RAND_MAX;

 // step 2, 
 unsigned int lastBit = random() % 2;
 lastBit = lastBit << ( __WORDSIZE/2-1);
 // This should never overflow, add a 31-bit number into a 32.
 randVal+= lastBit; 
 double fpRandVal = 1.0*randVal;
 return (randMax * fpRandVal / UINT_MAX);
}
	
unsigned int RandomInt(int randMax) {
  int randVal = (randMax * ((1.0*random()) / RAND_MAX));
  return std::min(randMax-1, randVal);
}

unsigned int RandomInt(unsigned int min, unsigned int max) {
	return RandomInt(max - min) + min;
}

float Random() {
	return (RandomInt(0,RAND_MAX)*1.0)/RAND_MAX;
}

bool FindQNorm(float prob, float & nStdDev) {
    if (prob < 0.5 or prob > 1.0) {
        return false;
    }
    else {
        nStdDev = qnorm[(int)((prob - 0.50) * 500)];
        return true;
    }
}