I'm building a project that does a giveaway every Monday. There are three prizes, the basic prize, the "lucky" prize, and the jackpot prize. The basic prize is given out 70% of the time, the lucky prize gets given out 25% of the time and the jackpot prize gets rewarded the final 5% of the time.
There are multiple people that get prizes each Monday. Each person in the giveaway gets at least the basic prize.
Right now I'm just generating a random number and then assigning the prizes to each participant on my local computer. This works, but the participants have to trust that I'm not rigging the giveaway.
I want to improve this giveaway by using the blockchain to be the random number generator. The problem is that I don't know how to do this technically.
Here is my start to figuring out how to do it: When the giveaway is created, a block height is defined as being the source of randomness. Each participant has a userID. When the specific block number is found, the block hash is catenated to the userID and then hashed. The resulting hash is then "ranged" across the odds defined in the giveaway.
The part I can't figure out is how to do the "ranging". I think it might involve the modulo operator, but I'm not sure. If the resulting hash falls into the 5% range, then that user gets the jackpot prize. If the hash falls into the 70% range, then he gets the basic prize, etc.
I figured this out, the solution is below:
int(hashlib.sha256(block_header + userID).hexdigest(), 16) / float(2**256)
You conver the hash into an integer, then divide that integer by 2**256. This gives you a decimal from 0 to 1 that can be compared to a random.random() to get the prize.
Assigning giveaways based on cryptocurrency block header