banner
0xMech

0xMech

Focus on various AI application sharing, Blockchain learners, web3 new leeks
twitter

Lottery based on blockchain random number and hash algorithm

Famous video website Bilibili's lottery may have only 1 non-member among the ten winners. In the lottery initiated by Wang Sicong, there may be only one non-beautiful woman among the 100 winners. Even the most basic gender fairness cannot be achieved. So how can we achieve a fair lottery?

b 站.png
wsc.JPEG

As an absolute lottery insulation body, I spent some time learning the principles of lottery in order to understand why I couldn't win. It turns out that achieving a fair and just lottery is not easy.

Live streaming lottery often uses the method of screenshotting to create an active atmosphere, but it cannot reproduce the lottery results. Some bloggers use the method of sorting the lottery range and generating a random number, for example, selecting one person out of 100 people by sorting them in a certain way and generating a random number from 0 to 100. However, this method cannot determine whether the random number is truly random, and participants cannot verify the fairness of the lottery. Even if the organizer provides a screenshot, it cannot prove the fairness of the lottery.

Using random numbers can ensure that each participant has an equal chance of winning in the lottery. Each participant's options are equivalent, and each option has an equal chance of being selected. Using random numbers can ensure this fairness.
At the same time, using random numbers can increase the fun and excitement of the lottery. Each participant does not know if they will be selected, which adds excitement and suspense to the lottery.
In order to ensure randomness during the lottery process, a high-quality random number generator needs to be used. A good random number generator should be unbiased, independent, uniform, and have periodicity. If the generated random numbers are not of high quality, it may result in unfair lottery results or high predictability.

We all know that computers cannot generate true random numbers, and all computer random numbers are pseudo-random. So I use the hash value of the first Bitcoin block header after a certain determined time point as the random number. Since I cannot mine, I cannot generate block headers, let alone manipulate the results of random number generation. The basic algorithm of the lottery uses the hash algorithm.
The specific process of implementation is as follows:

  1. Count the IDs of the participants.
  2. Determine a fixed time, such as 13:00 on April 22, 2023, in the UTC+8 time zone. Take the hash value of the first Bitcoin block header after this time as the random number.
  3. Take the hash value of each person's ID together with this random number.
  4. Sort the obtained hash values and determine the winners based on the number of winners.

I don't know how to write code, so I asked ChatGPT to help me write this code.
ChatGPT 写抽奖代码.png

After installing the required libraries, you can run it. The participant ID can be Bilibili UID, mobile phone number, or even a person's name. Here I will demonstrate using QQ numbers. Export the QQ numbers of the group members using the following two websites:
qq.png
zz.png

After exporting, you will get a .txt file consisting of all the QQ numbers. Then, based on the determined time, open the blockchain browser and find the block header hash value of the required block. Fill in this hash value and the .txt file name in the above program, and finally run this program. The first winner will be the prize winner.
hash.png
zj.png

The program used will be open source, and I will post it in the comments section.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.