The R Project SVN R

Rev

Rev 68947 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68947 Rev 76163
Line 37... Line 37...
37
{
37
{
38
    I1= 36969*(I1 & 0177777) + (I1>>16);
38
    I1= 36969*(I1 & 0177777) + (I1>>16);
39
    I2= 18000*(I2 & 0177777) + (I2>>16);
39
    I2= 18000*(I2 & 0177777) + (I2>>16);
40
    return ((I1 << 16)^(I2 & 0177777)) * 2.328306437080797e-10; /* in [0,1) */
40
    return ((I1 << 16)^(I2 & 0177777)) * 2.328306437080797e-10; /* in [0,1) */
41
}
41
}
-
 
42
 
-
 
43
#include <math.h>
-
 
44
#include <stdint.h>
-
 
45
//copied from src/main/RNG.c:
-
 
46
//generate a random non-negative integer < 2 ^ bits in 16 bit chunks
-
 
47
static double rbits(int bits)
-
 
48
{
-
 
49
    int_least64_t v = 0;
-
 
50
    for (int n = 0; n <= bits; n += 16) {
-
 
51
	int v1 = (int) floor(unif_rand() * 65536);
-
 
52
	v = 65536 * v + v1;
-
 
53
    }
-
 
54
    // mask out the bits in the result that are not needed
-
 
55
    return (double) (v & ((1L << bits) - 1));
-
 
56
}
-
 
57
 
-
 
58
double R_unif_index(double dn)
-
 
59
{
-
 
60
    // rejection sampling from integers below the next larger power of two
-
 
61
    if (dn <= 0)
-
 
62
	return 0.0;
-
 
63
    int bits = (int) ceil(log2(dn));
-
 
64
    double dv;
-
 
65
    do { dv = rbits(bits); } while (dn <= dv);
-
 
66
    return dv;
-
 
67
}