19 Commits
v1.0 ... v1.2

Author SHA1 Message Date
1e9ae43e52 update workflows (to also include ARM support) 2025-06-17 23:51:02 +02:00
95dce84d28 fully reoptimize 2025-06-17 23:51:02 +02:00
deabe97247 rework code to be more accurate and platform-independent 2025-06-17 12:55:20 +02:00
3bbfea7400 fix header file to be more platform-independent 2025-06-17 12:46:47 +02:00
Quinn
a37f47d9f6 fix: no such thing as $default-branch 2025-04-09 15:42:25 +02:00
Quinn
42264b6402 fix: compiler wasn't available 2025-04-09 15:35:53 +02:00
Quinn
6189bdcf92 fix: bear not available 2025-04-09 15:35:53 +02:00
Quinn
10d1aab05f add new gh workflows / most of it 2025-04-09 15:35:11 +02:00
Quinn
ef1dcf7c39 small fixes in the code, mainly to ensure things work properly. 2025-04-09 14:57:14 +02:00
Quinn
40c48980b4 use makefile build system 2025-04-09 14:56:24 +02:00
Quinn
28e8aeb707 fix: forgot newlime innlog message 2025-04-06 17:34:01 +02:00
Quinn
4607afed9a use 64 bit integers for count, rather than 32 bit.
Was facing issues with integer wrapping
2025-04-06 17:32:26 +02:00
Quinn
6ef3d4a660 add bounds to numbered input 2025-04-06 17:27:55 +02:00
Quinn
40aa1b59e6 add error management script 2025-04-06 17:23:22 +02:00
Quinn
50964df129 refactor and pedantic fixes 2025-04-06 17:22:56 +02:00
Quinn
8c8386740b Fix: no need for the if condition there
Instead check if no arguments were given and exit sooner.
2025-04-06 16:55:54 +02:00
Quinn
6acaff36f7 remove package directory 2025-04-06 15:55:47 +02:00
Quinn
1da9e4243e allow for parsing command line arguments for multiple coinflips at once 2025-04-06 15:55:33 +02:00
unset
c8dce9dc0e improved random seed to be less predictable 2025-01-30 16:09:23 +01:00
5 changed files with 128 additions and 21 deletions

34
.github/workflows/publish.yaml vendored Normal file
View File

@@ -0,0 +1,34 @@
name: publish
on:
release:
types: published
workflow_dispatch:
inputs:
release_tag:
required: true
description: the tag to release for
default: ""
jobs:
release_bin:
runs-on: ${{matrix.os}}
strategy:
matrix:
os:
- ubuntu-24.04
- ubuntu-24.04-arm
steps:
- uses: actions/checkout@v4
- run: make compile
- run: echo "FNAME=coinflip-$(uname -s)-$(uname -m)" >>"$GITHUB_ENV"
- run: mv bin/coinflip "$FNAME"
- uses: softprops/action-gh-release@v2
with:
tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
files: ${{env.FNAME}}
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- if: ${{env.ACT}}
uses: actions/upload-artifact@v4
with:
path: ${{env.FNAME}}

11
.gitignore vendored
View File

@@ -1,3 +1,8 @@
.vscode .*
build
package/usr !.github/
!.gitignore
/bin/
/obj/
/compile_commands.json

32
makefile Normal file
View File

@@ -0,0 +1,32 @@
CC ?= cc
CFLAGS += -Wall -Wextra -Wpedantic -O3 -MD -MP -std=gnu99
LDFLAGS +=
SRC = $(shell find src/ -name '*.c')
OBJ := $(patsubst src/%,obj/%,$(SRC:.c=.o))
BIN := bin/coinflip
compile: compile_commands.json $(BIN)
clean:
rm -rf obj/ bin/ compile_commands.json
# link the .o files
$(BIN): $(OBJ)
@mkdir -p $(@D)
$(CC) $(LDFLAGS) $(OBJ) -o $(BIN)
# compile .o files (w/ .d files)
obj/%.o: src/%.c
@mkdir -p $(@D)
$(CC) -c $(CFLAGS) -o $@ $<
ifneq ($(shell which bear),)
compile_commands.json: makefile
$(MAKE) clean
@touch compile_commands.json
bear -- make compile
else
compile_commands.json:
endif
-include $(OBJ:.o=.d)

View File

@@ -1,6 +0,0 @@
Package: coinflip
Version: 1.0
Architecture: all
Priority: optional
Maintainer: Quinn
Description: adds a coinflip command which has a 50% chance of printing "heads" and 50% chance of printing "tails"

View File

@@ -1,18 +1,60 @@
#include <bits/time.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <time.h>
int main(void) { #define error(ret, s, ...) \
srand(time(NULL)); // set the random seed to the system time do { \
char* result = NULL; fprintf(stderr, (s), ##__VA_ARGS__); \
exit(ret); \
} while (0)
if (rand() & (~1) != 0) { typedef unsigned long long ull;
result = "heads";
}
else { int main(int argc, char** argv) {
result = "tails"; // get the current time to set as the seed
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srandom(ts.tv_nsec);
// return the result if no input
if (argc <= 1)
return printf("%s\n", (rand() & 1) ? "heads" : "tails");
// loop through arguments
for (unsigned i = 1; i < (unsigned)argc; ++i) {
// parse the argument
errno = 0;
ull c = strtoull(argv[i], NULL, 10);
if (errno != 0) error(errno, "parse error for string: '%s'\n", argv[i]);
// perform for the input count
ull headsc = 0; // amount of heads
ull tailsc = 0; // amount of tails
while (c >= 31) {
c -= 31;
long n = random(); // generate a random number for this batch
int cnt = __builtin_popcountl(n); // counts the set bits
headsc += cnt;
tailsc += 31 - cnt;
} }
printf("%s\n", result); if (c > 0) {
long n = random();
long msk = (1 << c) - 1;
int cnt = __builtin_popcountl(n & msk);
headsc += cnt;
tailsc += c - cnt;
}
// print the results of this cycle
printf("results:\n heads: %llu\n tails: %llu\n", headsc, tailsc);
}
return 0; return 0;
} }