-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.cpp
64 lines (53 loc) · 1.09 KB
/
random.cpp
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
#include "random.h"
#include <stdlib.h>
void random_permutation(int n, int* a)
{
for (int i = 0; i < n-1; ++i) {
/**
* pick a random pos from a[i, n)
*/
int pos = rand();
if (pos < 0) pos = -pos;
pos %= (n-i);
/**
* swap:
* a[i] <-> a[p]
*/
int tmp = a[i];
a[i] = a[i + pos];
a[i+pos] = tmp;
}
}
/**
* precondition: a[0, n) has no duplicate
*/
void random_subset(int n, int* a, int m, int* b)
{
if (m == 0)
return;
if (n == 0)
return;
random_subset(n-1, a, m-1, b);
int pos = rand();
if (pos < 0) pos = -pos;
pos %= n;
int elem = a[pos];
bool has_elem = false;
for (int i = 0; i < m-1; ++i) {
if (b[i] == elem) {
has_elem = true;
break;
}
}
if (has_elem)
elem = a[n-1];
b[m-1] = elem;
}
/**
$ for i in {1..200}; do t/random/random.play -s$RANDOM -as 4 {1..5}; done | sort | uniq -c
41 1 2 3 4
30 1 2 3 5
40 1 2 4 5
46 1 3 4 5
43 2 3 4 5
*/