Hey everyone,
It's time for another "Fun with Python" post. A while back, I shared a Terminal Dice Roller that used Hive to generate random, deterministic results. Today, we're going to use that same concept to shuffle a deck of cards and draw from it right in the terminal.
How it Works: Seeding with the Blockchain
The most interesting part of this script is how it generates a "random" result that is both unpredictable beforehand and verifiable after the fact. It does this by creating a seed based on live data from Hive blocks.
- Fetch the Latest Blocks: The script uses my
hive-nectar
library to get the last two blocks produced on the Hive blockchain. - Create a Seed String: It then creates a long string of text by concatenating the block ID of the first block, the block ID of the second block, and the transaction ID of a random transaction found in that second block.
- Hash the String: This long string is then hashed using SHA-256, which produces a unique and very large number.
- Seed the Draw: This number is used as the seed for Python's
random
module. This means that for the exact same two blocks, the shuffle and draw will always produce the same result, making it provably fair.
Note: If this were taking place as part of a transaction I would use that block, the previous block and that transaction instead of random one.
The Code
The script uses hive-nectar
to communicate with the Hive and the rich
library to create the nicely formatted table you see in the screenshot. (Eagle-eyed readers might get a pretty decent idea of what I'm working on from the context of this post and the image.)
To build a deck of cards:
def build_deck(include_jokers: bool = False) -> List[str]:
"""Return a list of card labels like 'A♠', '10♥', 'K♦', '3♣'."""
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = [("♠", "Spades"), ("♥", "Hearts"), ("♦", "Diamonds"), ("♣", "Clubs")]
deck = [f"{r}{s[0]}" for r in ranks for s in suits]
if include_jokers:
deck += ["Joker🃏", "Joker🃏"]
return deck
You can find the full script at the Gist linked below, and will be added to the project builder soon, as well as the dice one.
As always,
Michael Garcia a.k.a. TheCrazyGM
I love the fact that there are always more, fun legos to add to the peoject builder!
!PAKX
!PIMP
!PIZZA
View or trade
PAKX
tokens.Use !PAKX command if you hold enough balance to call for a @pakx vote on worthy posts! More details available on PAKX Blog.
$PIZZA slices delivered:
@ecoinstant(1/20) tipped @thecrazygm
Come get MOONed!
The way you tie the seed too the last two blocks plus a tx id is brilliant. As a numbers guy that really clicks for me, its audit friendly and and easy to reproduce later. The flow feels clean and simple all around. Cant wait too see where you take this :)
But wait , it can't right?
Because the txid is chosen randomly each time?
@thecrazygm
if you use the same sha256 seed that it renders, like the one in the image it will get the same result. In a real use case I would think the tx_id would be static because it would be using the tx of the custom_json it was called in.
You are right though, if you call it with just the block info it would call a new random tx,
For provable purposes, i would log the 3 ids it used and the sha256 to compare against later (which is what the thing I'm playing with now does, adds the actual seed to the database for comparison checks ).
Alright makes sense. Got it. What will you do with this? Just a side project or are you gonna let people gamble?
Originally I had it using the first transaction for the roll. But if 2 people roll at the same they would get the same results, which is why I moved to random TX in block.
GOt you
That's another fun tool to add to the list! All these modular tools sound like they could be combined in various ways to provide various functioning. 😁🙏💚✨🤙
That is 100% the idea! Even if I don't make something of them, I really hope it at least inspires someone else to grab the reigns and build something awesome!
I love that so much! Hearing that makes me think that I really, really should finally actually learn to code, because I have a lot that I want to build, and having modular tools like this available would make my task all the easier! I really appreciate you, my friend! 😁🙏💚✨🤙
Congratulations @thecrazygm! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out our last posts: