Miscellaneous scripts
This repository contains miscellaneous scripts that does not fit in one repository, yet I will use them sometimes for my personal use. Note that some of the scripts might contain hardcoded paths and opinionated presets, and you are advised to inspect them before actually using.
Loading...
Searching...
No Matches
empiricially.c
Go to the documentation of this file.
1#include <time.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <assert.h>
5
6
7/* Returns an integer in the range [0, n).
8 *
9 * Uses rand(), and so is affected-by/affects the same seed.
10 */
11int randint(int n) {
12 if ((n - 1) == RAND_MAX) {
13 return rand();
14 } else {
15 // Supporting larger values for n would requires an even more
16 // elaborate implementation that combines multiple calls to rand()
17 assert (n <= RAND_MAX);
18
19 // Chop off all of the values that would cause skew...
20 int end = RAND_MAX / n; // truncate skew
21 assert (end > 0);
22 end *= n;
23
24 // ... and ignore results from rand() that fall above that limit.
25 // (Worst case the loop condition should succeed 50% of the time,
26 // so we can expect to bail out of this loop pretty quickly.)
27 int r;
28 while ((r = rand()) >= end);
29
30 return r % n;
31 }
32}
33
34int main(){
35 int a,b,c=0;
36 srand((unsigned int) time(NULL));
37 for (int d = 0; d<1000000; d++)
38 {
39 if (a==4) {b++;}
40 else
41 {
42 if (b==3) {a++;}
43 else {
44 if (randint(2)==1) {a++;}
45 else {b++;}
46 }
47 }
48 if ((a==4) && (b==2))
49 {
50 c++;
51 a=0;
52 b=0;
53 }
54 if ((a==4) && (b==3))
55 {
56 a=0;
57 b=0;
58 }
59 }
60 printf("%d", 1000000 / c);
61 return 0;
62}
int randint(int n)
int main()