|
Szabolcs Nagy |
cfa23c |
#include <float.h>
|
|
Szabolcs Nagy |
cfa23c |
#include <stdint.h>
|
|
Szabolcs Nagy |
cfa23c |
#include <stdlib.h>
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
// TODO: use large period prng
|
|
Szabolcs Nagy |
cfa23c |
static uint64_t seed = -1;
|
|
Szabolcs Nagy |
cfa23c |
static uint32_t rand32(void)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
seed = 6364136223846793005ULL*seed + 1;
|
|
Szabolcs Nagy |
cfa23c |
return seed >> 32;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
static uint64_t rand64(void)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
uint64_t u = rand32();
|
|
Szabolcs Nagy |
cfa23c |
return u<<32 | rand32();
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
static double frand()
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
return rand64() * 0x1p-64;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
static float frandf()
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
return rand32() * 0x1p-32f;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
static long double frandl()
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
return rand64() * 0x1p-64L
|
|
Szabolcs Nagy |
cfa23c |
#if LDBL_MANT_DIG > 64
|
|
Szabolcs Nagy |
cfa23c |
+ rand64() * 0x1p-128L
|
|
Szabolcs Nagy |
cfa23c |
#endif
|
|
Szabolcs Nagy |
cfa23c |
;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
void t_randseed(uint64_t s)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
seed = s;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* uniform random in [0,n), n > 0 must hold */
|
|
Szabolcs Nagy |
cfa23c |
uint64_t t_randn(uint64_t n)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
uint64_t r, m;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* m is the largest multiple of n */
|
|
Szabolcs Nagy |
cfa23c |
m = -1;
|
|
Szabolcs Nagy |
cfa23c |
m -= m%n;
|
|
Szabolcs Nagy |
cfa23c |
while ((r = rand64()) >= m);
|
|
Szabolcs Nagy |
cfa23c |
return r%n;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* uniform on [a,b], a <= b must hold */
|
|
Szabolcs Nagy |
cfa23c |
uint64_t t_randint(uint64_t a, uint64_t b)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
uint64_t n = b - a + 1;
|
|
Szabolcs Nagy |
cfa23c |
if (n)
|
|
Szabolcs Nagy |
cfa23c |
return a + t_randn(n);
|
|
Szabolcs Nagy |
cfa23c |
return rand64();
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* shuffle the elements of p and q until the elements in p are well shuffled */
|
|
Szabolcs Nagy |
cfa23c |
static void shuffle2(uint64_t *p, uint64_t *q, size_t np, size_t nq)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
size_t r;
|
|
Szabolcs Nagy |
cfa23c |
uint64_t t;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
while (np) {
|
|
Szabolcs Nagy |
cfa23c |
r = t_randn(nq+np--);
|
|
Szabolcs Nagy |
cfa23c |
t = p[np];
|
|
Szabolcs Nagy |
cfa23c |
if (r < nq) {
|
|
Szabolcs Nagy |
cfa23c |
p[np] = q[r];
|
|
Szabolcs Nagy |
cfa23c |
q[r] = t;
|
|
Szabolcs Nagy |
cfa23c |
} else {
|
|
Szabolcs Nagy |
cfa23c |
p[np] = p[r-nq];
|
|
Szabolcs Nagy |
cfa23c |
p[r-nq] = t;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* shuffle the elements of p */
|
|
Szabolcs Nagy |
cfa23c |
void t_shuffle(uint64_t *p, size_t n)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
shuffle2(p,0,n,0);
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
void t_randrange(uint64_t *p, size_t n)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
size_t i;
|
|
Szabolcs Nagy |
cfa23c |
for (i = 0; i < n; i++)
|
|
Szabolcs Nagy |
cfa23c |
p[i] = i;
|
|
Szabolcs Nagy |
cfa23c |
t_shuffle(p, n);
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* hash table insert, 0 means empty, v > 0 must hold, len is power-of-2 */
|
|
Szabolcs Nagy |
cfa23c |
static int insert(uint64_t *tab, size_t len, uint64_t v)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
size_t i = v & (len-1);
|
|
Szabolcs Nagy |
cfa23c |
size_t j = 1;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
while (tab[i]) {
|
|
Szabolcs Nagy |
cfa23c |
if (tab[i] == v)
|
|
Szabolcs Nagy |
cfa23c |
return -1;
|
|
Szabolcs Nagy |
cfa23c |
i += j++;
|
|
Szabolcs Nagy |
cfa23c |
i &= len-1;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
tab[i] = v;
|
|
Szabolcs Nagy |
cfa23c |
return 0;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* choose k unique numbers from [0,n), k <= n */
|
|
Szabolcs Nagy |
cfa23c |
int t_choose(uint64_t n, size_t k, uint64_t *p)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
uint64_t *tab;
|
|
Szabolcs Nagy |
cfa23c |
size_t i, j, len;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
if (n < k)
|
|
Szabolcs Nagy |
cfa23c |
return -1;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
if (n < 16) {
|
|
Szabolcs Nagy |
cfa23c |
/* no alloc */
|
|
Szabolcs Nagy |
cfa23c |
while (k)
|
|
Szabolcs Nagy |
cfa23c |
if (t_randn(n--) < k)
|
|
Szabolcs Nagy |
cfa23c |
p[--k] = n;
|
|
Szabolcs Nagy |
cfa23c |
return 0;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
if (k < 8) {
|
|
Szabolcs Nagy |
cfa23c |
/* no alloc, n > 15 > 2*k */
|
|
Szabolcs Nagy |
cfa23c |
for (i = 0; i < k;) {
|
|
Szabolcs Nagy |
cfa23c |
p[i] = t_randn(n);
|
|
Szabolcs Nagy |
cfa23c |
for (j = 0; p[j] != p[i]; j++);
|
|
Szabolcs Nagy |
cfa23c |
if (j == i)
|
|
Szabolcs Nagy |
cfa23c |
i++;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
return 0;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
// TODO: if k < n/k use k*log(k) solution without alloc
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
if (n < 5*k && (n-k)*sizeof *tab < (size_t)-1) {
|
|
Szabolcs Nagy |
cfa23c |
/* allocation is n-k < 4*k */
|
|
Szabolcs Nagy |
cfa23c |
tab = malloc((n-k) * sizeof *tab);
|
|
Szabolcs Nagy |
cfa23c |
if (!tab)
|
|
Szabolcs Nagy |
cfa23c |
return -1;
|
|
Szabolcs Nagy |
cfa23c |
for (i = 0; i < k; i++)
|
|
Szabolcs Nagy |
cfa23c |
p[i] = i;
|
|
Szabolcs Nagy |
cfa23c |
for (; i < n; i++)
|
|
Szabolcs Nagy |
cfa23c |
tab[i-k] = i;
|
|
Szabolcs Nagy |
cfa23c |
if (k < n-k)
|
|
Szabolcs Nagy |
cfa23c |
shuffle2(p, tab, k, n-k);
|
|
Szabolcs Nagy |
cfa23c |
else
|
|
Szabolcs Nagy |
cfa23c |
shuffle2(tab, p, n-k, k);
|
|
Szabolcs Nagy |
cfa23c |
free(tab);
|
|
Szabolcs Nagy |
cfa23c |
return 0;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* allocation is 2*k <= len < 4*k */
|
|
Szabolcs Nagy |
cfa23c |
for (len = 16; len < 2*k; len *= 2);
|
|
Szabolcs Nagy |
cfa23c |
tab = calloc(len, sizeof *tab);
|
|
Szabolcs Nagy |
cfa23c |
if (!tab)
|
|
Szabolcs Nagy |
cfa23c |
return -1;
|
|
Szabolcs Nagy |
cfa23c |
for (i = 0; i < k; i++)
|
|
Szabolcs Nagy |
cfa23c |
while (insert(tab, len, t_randn(n)+1));
|
|
Szabolcs Nagy |
cfa23c |
for (i = 0; i < len; i++)
|
|
Szabolcs Nagy |
cfa23c |
if (tab[i])
|
|
Szabolcs Nagy |
cfa23c |
*p++ = tab[i]-1;
|
|
Szabolcs Nagy |
cfa23c |
free(tab);
|
|
Szabolcs Nagy |
cfa23c |
return 0;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|