 # What Determines Whether you Win at a Blockchain Casino?

The key factor that determines the result of a blockchain online casino game is the random value. There are various ways to obtain a random number in other programs, but blockchain casino games are different from the random value generation method in other programs. With EOS casinos and ETH casinos there is no such function in the programming language. Since several nodes have to execute smart contracts to verify the validity of the transaction, in order to reach an agreement, all execution processes must be determined, which is contrary to randomness.

Therefore, the acquisition of random values in smart contracts is mainly completed in the following three ways:

(1) Generated from data on other chains

(2) Provably fair random numbers obtained from interactions between non-trusting parties

(3) Off-chain data obtained by “oracle machine”

1. Generated random numbers from data on other chains

This is the easiest way to generate random numbers. It only needs to use the data of the existing blockchain. This is the smart contract that can directly obtain the data. Unfortunately, this is also the most insecure and most predictable method of random number generation, because the random number can be determined from publicly available on-chain data. You can call multiple data sources as seed numbers – the harder it is to predict the better. For example

(1) Current block time

(2) Block number

(3) Other smart contract data, such as current RAM prices in EOS.

This is the simplest method of generating random numbers and is generally used in non-critical areas.

2. Provably fair random numbers obtained from interactions between non-trusting parties

The random number is coming from more complex algorithms. The data needs to rely on the off-chain to inject smart contracts through an action. This section discusses providing independent random numbers from both parties (or multiple parties) to generate shared random numbers. See below:

Jack and Rose each submit a random number to the smart contract, and calculate r = x ⊕ y (⊕ : OR).

The biggest advantage of this method is that even if either Jack or Rose does not provide a random number, the calculation result r is still random. That is to say, even if the parties involved do not trust each other, they can still get random numbers, as long as one party is honest. In a dice game on the blockchain, as long as the user generates a random number, they can believe that the random number is fair and has not been manipulated by a blockchain, ETH, or EOS casino. The same is true for institutions.

The reason for ensuring fairness is that we are essentially calculating the cryptographic one-time part, which can be proved mathematically.

For this simple algorithm, there is still a flaw. Ideally, Jack and Rose submit random numbers to the smart contract at the same time, but in reality this is impossible. This may allow the submitter to control the random number result.

If Jack first submits a random number x to the gambling smart contract, Rose can force any value of r’ that makes him win the game by submitting y = r’ ⊕ x. The smart contract will calculate:

r = x ⊕ y = x ⊕ (r’ ⊕ x) = (x ⊕ x) ⊕ r’ = 0⊕ r’= r’

We need to hide the value of the contract entered at first so that Rose cannot cheat. In order to achieve this, there is another cryptography concept – commitment scheme.

Jack calculates a random number, encrypts it, and passes the encrypted random number to the smart contract. After Rose submits the random value to the smart contract, Jack then publishes the unencrypted random value to the smart contract and finally calculates a shared random value. In addition to the hidden value, another value of the promise method is binding. Binding means that Jack can only publish the random value he submits, not other numbers. This also prevents Jack from cheating by seeing the random number transmitted by Rose. It is very easy to use the hash algorithm to achieve the commitment scheme by a collision-resistant hash function.

In order to promise that the value of x will be disclosed in the future, JACK calculates c = H (x), where H is the hash algorithm. In order to verify whether the value transmitted by Jack is x, you only need to verify H (x) again. See below:

This algorithm is relatively safe. Random numbers cannot be predicted and can guarantee fairness to all parties. It is widely used in various blockchain casino games and applications. The disadvantage is that it requires at least two parties and three rounds of interaction to generate random numbers. Of course, one of the parties may be the server and has been responding automatically. This is how many betting programs use it, but it also comes at the expense of decentralization.

3. Off-chain data obtained by “oracle machine”

Another interesting method is to grab random numbers through Oracle. “The oracle machine” is a method of introducing external data into the blockchain. It is also possible to obtain random numbers by requesting external trusted websites, such as wolfram alpha or random.org. The advantage is that there is no need to interact with multiple parties. The disadvantage is that the company that runs the “oracle machine” needs to charge a small handling fee, and needs to wait for a response.