Skip to content

user21944/coinflip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

coinflip

elincyt

08/24/2023 12:59 PM
make C program that flip coin

A friend of mine who is learning Python made a program that flips an imaginary coin, printing the result but also the ratio between the number of times each side has landed (expressed as a percentage):

import random
heads = tails = 0
while True:
    input()
    if random.randrange(1,3) == 1: 
        print("Heads!")
        heads += 1
    else: 
        print("Tails!")
        tails += 1
    print(int(heads/(heads + tails)*100), "/", int(tails/(heads + tails)*100))

My immediate response was to rewrite it in C...
without the standard library

I've been experimenting with libc-less programs for some time now, and used this as an opportunity to create a simple, buffered and formatted output library using only Linux/BSD syscalls.

Building without libc (try NOLIBC=1 ./build.sh, only Linux x86_64 supported) creates an impressively small binary (0.98 KiB on my system), far smaller than the source code itself.

About

flip a coin (0.98 KiB, no dependencies, no libc, I wrote my own buffered output and other stuff, etc, etc.)

Resources

License

Stars

Watchers

Forks