March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -8,7 +8,7 @@ auto sofN_creator(in int n) {
i++;
if (i <= n)
sample ~= item;
else if (uniform(0.0, 1.0) < (cast(double)n / i))
else if (uniform(0.0, 1.0) < (double(n) / i))
sample[uniform(0, n)] = item;
return sample;
};
@ -18,12 +18,12 @@ void main() {
enum nRuns = 100_000;
size_t[10] bin;
foreach (trial; 0 .. nRuns) {
auto sofn = sofN_creator(3);
foreach (immutable trial; 0 .. nRuns) {
immutable sofn = sofN_creator(3);
int[] sample;
foreach (item; 0 .. bin.length)
foreach (immutable item; 0 .. bin.length)
sample = sofn(item);
foreach (s; sample)
foreach (immutable s; sample)
bin[s]++;
}
writefln("Item counts for %d runs:\n%s", nRuns, bin);

View file

@ -1,7 +1,7 @@
import std.stdio, std.random, std.algorithm;
double random01(ref Xorshift rng) {
immutable r = rng.front / cast(double)rng.max;
immutable r = rng.front / double(rng.max);
rng.popFront;
return r;
}
@ -14,7 +14,7 @@ struct SOfN(size_t n) {
i++;
if (i <= n)
sample[i - 1] = item;
else if (rng.random01 < (cast(double)n / i))
else if (rng.random01 < (double(n) / i))
sample[uniform(0, n, rng)] = item;
return sample[0 .. min(i, $)];
}

View file

@ -3,33 +3,32 @@
typedef NSArray *(^SOfN)(id);
SOfN s_of_n_creator(int n) {
NSMutableArray *sample = [NSMutableArray arrayWithCapacity:n];
NSMutableArray *sample = [[NSMutableArray alloc] initWithCapacity:n];
__block int i = 0;
return [[^(id item) {
return [^(id item) {
i++;
if (i <= n) {
[sample addObject:item];
} else if (rand() % i < n) {
[sample replaceObjectAtIndex:rand() % n withObject:item];
sample[rand() % n] = item;
}
return sample;
} copy] autorelease];
} copy];
}
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSCountedSet *bin = [[NSCountedSet alloc] init];
for (int trial = 0; trial < 100000; trial++) {
SOfN s_of_n = s_of_n_creator(3);
NSArray *sample;
for (int i = 0; i < 10; i++)
sample = s_of_n(@(i));
[bin addObjectsFromArray:sample];
}
NSLog(@"%@", bin);
NSCountedSet *bin = [[NSCountedSet alloc] init];
for (int trial = 0; trial < 100000; trial++) {
SOfN s_of_n = s_of_n_creator(3);
NSArray *sample;
for (int i = 0; i < 10; i++)
sample = s_of_n([NSNumber numberWithInt:i]);
[bin addObjectsFromArray:sample];
}
NSLog(@"%@", bin);
[bin release];
[pool release];
return 0;
}

View file

@ -1,29 +1,29 @@
#lang racket
#lang racket/base
(define (s-of-n-creator n)
(let* ([count 0] ; 'i' in the description
(let ([count 0] ; 'i' in the description
[vec (make-vector n)]) ; store the elts we've seen so far
(lambda (item)
(if (< count n)
; we're not full, so, kind of boring
(begin
(vector-set! vec count item)
(set! count (+ count 1))
(vector-copy vec 0 count))
(set! count (+ count 1)))
; we've already seen n elts; fun starts
(begin
(set! count (+ count 1))
(when (< (random) (/ n count))
(vector-set! vec (random n) item))
(vector-copy vec))))))
(when (< (random count) n)
(vector-set! vec (random n) item))))
vec)))
(define counts (make-vector 10))
(define counts (make-hash '((0 . 0) (1 . 0) (2 . 0) (3 . 0) (4 . 0) (5 . 0) (6 . 0) (7 . 0) (8 . 0) (9 . 0))))
(for ([iter (in-range 0 100000)]) ; trials
(let ([s-of-n (s-of-n-creator 3)]) ; set up the chooser
(for ([d (in-vector ; iterate over the chosen digits
(for/last ([digit (in-range 0 10)]) ; loop through the digits
(s-of-n digit)))]) ; feed them in
(hash-update! counts d add1)))) ; update counts
(vector-set! counts d (add1 (vector-ref counts d)))))) ; update counts
(for ([d (in-range 0 10)])
(printf "~a ~a~n" d (hash-ref counts d)))
(printf "~a ~a~n" d (vector-ref counts d)))