-
Notifications
You must be signed in to change notification settings - Fork 9
/
dist.cc
76 lines (59 loc) · 1.38 KB
/
dist.cc
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "dist.h"
#include <limits.h>
//------------------------------- RandSeed -------------------------------
UniformDist *RandSeed::ran1 = new UniformDist(INIT_SEED);
void RandSeed::set_seed(long new_seed)
{
delete ran1;
ran1 = new UniformDist(new_seed);
}
long RandSeed::new_seed(void)
{
float ans;
ans = (*ran1)();
ans *= (*ran1)();
// cout << "rand_seed : " << idum << " " << long(-LONG_MAX * ans) << endl;
return long(-LONG_MAX * ans);
}
//------------------------------- Choose -------------------------------
// allows selection of k random items from the string
//
Choose::Choose(LINT n, LINT k)
{
UniformDist ud;
LINT i, j, ival;
FLOAT fval;
num = new LINT [n];
rval = new FLOAT [n];
// associate a random value with each item
// also copy item into num
for (i = 0; i < n; i++)
{
rval[i] = ud();
num[i] = i;
}
// sort num according to the values in rval
for (i = 0; i < n; i++ )
{
ival = num[i]; fval = rval[i];
for ( j = i; j > 0 && rval[j-1] > fval; j-- ) {
num[j] = num[j-1];
rval[j] = rval[j-1];
}
num[j] = ival;
rval[j] = fval;
}
// resort first k num according to position
for (i = 0; i < k; i++ )
{
ival = num[i];
for ( j = i; j > 0 && num[j-1] > ival; j-- )
num[j] = num[j-1];
num[j] = ival;
}
}
Choose::~Choose(void)
{
delete [] num;
delete [] rval;
}