Bitroller
Published © CC BY

Dice tester and random number generator

This is a machine that rolls dice, reads them using Python, Open CV and Raspberry Pi and then stores the values.

BeginnerShowcase (no instructions)16 hours27

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Camera Module
Raspberry Pi Camera Module
×1
DC Motor, 12 V
DC Motor, 12 V
×1

Software apps and online services

OpenCV
OpenCV

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Parts for the dice thrower

STL parts, printed with PLA

Code

Dice reading with OpenCV

Python
This code operates on the Raspberry Pi 4 with Picamera 2.1, and a BC547c transistor on GPIO 21 that controls the electrical motor that stirs the dice.
def read_numbers_from_file(filename):
    with open(filename, "r") as f:
        data = f.read().split()

    numbers = [int(x) for x in data]

    if len(numbers) < 2:
        raise ValueError("Need at least two numbers")

    return numbers


def von_neumann_extraction(numbers):
    bits = []

    for i in range(0, len(numbers) - 1, 2):
        x = numbers[i]
        y = numbers[i + 1]

        if x == y:
            continue
        elif x < y:
            bits.append(0)
        else:
            bits.append(1)

    return bits


def main():
    filename = "random_numbers.txt"

    numbers = read_numbers_from_file(filename)
    bits = von_neumann_extraction(numbers)

    print("Input numbers:")
    print(numbers)

    print("\nVon Neumann extracted bits:")
    print(bits)

    print("\nNumber of bits:", len(bits))


if __name__ == "__main__":
    main()

Credits

Bitroller
2 projects • 1 follower

Comments