summaryrefslogtreecommitdiffstats
path: root/jni/feature_stab/db_vlvm/db_utilities_random.h
blob: ef24039c1ea72091bbd7a8ae9966b79ec0fc93fc (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id: db_utilities_random.h,v 1.1 2010/08/19 18:09:20 bsouthall Exp $ */

#ifndef DB_UTILITIES_RANDOM
#define DB_UTILITIES_RANDOM

#include "db_utilities.h"



/*****************************************************************
*    Lean and mean begins here                                   *
*****************************************************************/
/*!
 * \defgroup LMRandom (LM) Random numbers, random sampling
 */
/*\{*/
/*!
 Random Number generator. Initialize with non-zero
integer value r. A double between zero and one is
returned.
\param r    seed
\return random double
*/
inline double db_QuickRandomDouble(int &r)
{
    int c;
    c=r/127773;
    r=16807*(r-c*127773)-2836*c;
    if(r<0) r+=2147483647;
    return((1.0/((double)2147483647))*r);
    //return (((double)rand())/(double)RAND_MAX);
}

/*!
Random Number generator. Initialize with non-zero
integer value r. An int between and including 0 and max
 \param r    seed
 \param max    upped limit
 \return random int
*/
inline int db_RandomInt(int &r,int max)
{
    double dtemp;
    int itemp;
    dtemp=db_QuickRandomDouble(r)*(max+1);
    itemp=(int) dtemp;
    if(itemp<=0) return(0);
    if(itemp>=max) return(max);
    return(itemp);
}

/*!
 Generate a random sample indexing into [0..pool_size-1].
 \param s            sample (out) pre-allocated array of size sample_size
 \param sample_size    size of sample
 \param pool_size    upper limit on item index
 \param r_seed        random number generator seed
 */
inline void db_RandomSample(int *s,int sample_size,int pool_size,int &r_seed)
{
    int temp,temp2,i,j;

    for(i=0;i<sample_size;i++)
    {
        temp=db_RandomInt(r_seed,pool_size-1-i);

        for(j=0;j<i;j++)
        {
            if(s[j]<=temp) temp++;
            else
            {
                /*swap*/
                temp2=temp;
                temp=s[j];
                s[j]=temp2;
            }
        }
        s[i]=temp;
    }
}
/*\}*/
#endif /* DB_UTILITIES_RANDOM */