ZMINE BLOG

English English   ไทย ไทย

How ZMINE generate random numbers from Smart Contract (2)

Share this article

As mentioned from the previous article in the first part about the different random processes.

In this article, we will demonstrate the Random type that ZMINE used to generate the prize for ST Reward.

Design

The rules of the Random is must be able to random values between numbers 1 to n, where n is the number of rights of ST Rewards in that round.

  1. Transparent and traceable – This feature is the basis of Blockchain technology. Therefore, the selection of Smart Contracts in the Ethereum Network can be a good choice.
  2. Difficult to predict – or find Seed that cannot be easily predictable in this section, we have designed to use both seed formation as True Random Number Generator and the part that creates seed as Pseudo-Random Number Generator combined together.
  3. Easy and fast – The method should not be complicated and able to provide immediate results. Therefore, we eliminated the use of Random methods using the Third-party API (such as random.org) or the random with highly complex and time-consuming like RANDAO go out
  4. Amount and duration of data usage – The random will be done twice a month, at every 1st and 16th of each month. Amount of numbers about 10-20 numbers per time

As mentioned above, we have designed the source of the seed as follows.

  1. Counter (Nonce) is a variable. The number of each random times, this number will increase by one every time that random is successful.
  2. Variables in Ethereum Network such as a variant of Block No. (number of the previous block) and Block Difficulty Which has changed in every block that occurs. Both of these variables are considered as variables that can be used easily when writing Smart Contract. We can call these variables out to use at all. However, these variables are easily predictable variables.
  3. Third-party Random We chose to use Lottery results, because it is reliable random, and the period that corresponds to ST Rewards must be used on the 1st and 16th of the month.

Once the seed source has been created, all 3 sources will then be used to generate a random number through Hash (Algorithm KECCAK-256). Which will result in number unit256. This hash will produce a relatively distributed result. That is to say, even though the Seed number will change to just one digit but the result of Hash will come out very different.

After getting the Hash results, then we will put it in the numerical range that we want (1 to n) by Modulo (%)

Smart Contract

We’ve written a Smart Contract named ZmineRandom for Random ST Rewards. Can be checked from URL https://etherscan.io/address/0xF912eFb4E59Cd2910D90a78b1c1491a870c54b12 which has verified source code already

ZmineRandom will do random values, especially those that are only eligible to do Random (Authorizers). When making Random, we will collect the seed used. In order to be able to trace back

Information on Smart Contract: ZmineRandom on Etherscan

From the Code we will see that, Random will assign 3 variables including;

  1. min: Random values (integers) are the lowest possible.
  2. max: Random values (integers) that have the maximum possible.
  3. lotto: Random numbers from outside (Government lottery results, chased away from the prize at 1,2,3 respectively)

When randomizing, the result will be stored at randomResultMap and stored seed in randomInputMap. Which can be checked from the ReadContract in Etherscan

Note: From Source Code above, we could notice that each seed, when passed the hash, will bring “+” together, which must be careful because this may cause overflow of numbers because uint256 has the highest value is 2 ** 256-1 or about 1.157920892373162e + 77 This may look like various of numbers but the results of the Hash often give the same numbers. For instance

uint256(keccak256(abi.encodePacked(1))) = 8.008442285988055e+76
uint256(keccak256(abi.encodePacked(2))) = 2.910267648167304e+76

uint256(keccak256(abi.encodePacked(3))) = 8.790302987107592e+76

Can see that only 3 of these are combined overflow, so + – x% in Smart Contract should always use the SafeMath Library to prevent overflow or arithmetic error, such as dividing by 0, etc.

However, because this Smart Contract has the purpose of “Random”, so it does not care whether or not the overflow will occur even better 🙂

Example of reading values from Smart Contract via Etherscan

The 1st Random ST Rewards, 9 prizes. The results are as follows.

Counter TxID Min Max ThirdParty Random No. Random Result
#1 0x73722ce3ada144c1845e97b7ff9a6106c65b44aba5e5687b5d6bcc689ffd197b 1 320,261 452643 037425
#2 0x58ba0a9731e1fae4e5c1fe7fc93131db4fce617163ce58ebcb9aa8216a31df90 1 320,261 261071 293284
#3 0x680e2ce365568085f3716834f61d4e7f51b0495ee4709d5caee9f47d9466adf3 1 320,261 350893 055588
#4 0x656862b0cccb8dd3ba405672868f11c706463dac9bacd488e69f8f76a209e639 1 320,261 616843 267931
#5 0x5be1ee99be7ac4f67950eab68f6c34f9badf950b61fe982068a5411b7feadd8c 1 320,261 875042 056760
#6 0x76affdae124a196cdfc3b784a2916591d3097adf7ed954abcca68b6a7e3e1606 1 320,261 964263 076413
#7 0x7ec93b92772d745ad2db965ee3e5cfb52a6099d40f78c14e5faee5f7b5183bf3 1 320,261 021460 078133
#8 0x69fa2cc01ae5f08b66c608be70e854d27e084a687eb5a75e6604f3ec7a69cc8f 1 320,261 315781 289283
#9 0x1cc67b412c42097b5bbb1c351280d076dfa67861107c7f4f1516a9c4905feba9 1 320,261 239381 275876

From tab Read Contract on Etherscan

2. Counter is to say that there are 9 times already been randomized.

 

 

 

 

4. randomResultMap Is the result of random results from the first random image, giving 37425 results

 

 

 

 

 

 

 

 

6. randomInputMap is the collection of all seeds used in Random

  • Index 0: Min
  • Index 1: Max
  • Index 2: Lotto
  • Index 3: Block Difficulty
  • Index 4: Block No

 

For Lotto numbers, can be checked from the latest Thai lottery results such as ST Reward matches the lottery results for 1 October 2018, respectively, 1st, 2nd, 3rd prize …

Note: Random #8 use 315781 and Random #9 use 239381 Switched order due to minor errors: P

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Summary

ST Rewards is a random prize distribution. Which the ZMINE team has focused on transparency and want everyone to be able to trace back. Therefore, we had adopted the Smart Contract principle to randomly and collect historical data.

Hope this article will be useful to everyone 🙂