Random Number at Runtime — NEO SmartContract

Fabio Henrique Cardoso
Coinmonks
3 min readAug 19, 2018

--

I’ve read this post: Using Random Numbers in Neo Smart Contracts

And after read that i come up with a problem. If we send two transaction in the same block using the code presented above?

Well, The answer is that it’ll have the same number in all transaction we have inside the block.

New Problem: How to get a random number per transaction inside NEO network?

The answer that i was looking forward just born with a conversation with Daniel Pan.

The Smart Contract

Compile the code above to get the AVM

00c56b5861650700616c756652c56b6c766b00527ac46168295379737465
6d2e457865637574696f6e456e67696e652e476574536372697074436f6e
7461696e65726c766b51527ac46168184e656f2e426c6f636b636861696e
2e4765744865696768746168184e656f2e426c6f636b636861696e2e4765
744865616465726168124e656f2e4865616465722e476574486173686c76
6b51c36168174e656f2e5472616e73616374696f6e2e476574486173687e
aa006c766b00c37f616c7566

After deploy the contract on some privatenet, we can now invoke that smart contract and see the result.

The invocation

I managed to put two transactions on the same block as follows:

Checking the result of these two transactions:

TX1: e01d0e5f9c2e1bf5b4205386f9a68cfa066b0904bc789e9e86e7a5d8b803c98a Result: 8ac903b8d8a5e7869e9e78bc04096b06fa8ca6f9865320b4f51b2e9c5f0e1de0

TX2: b66abee7a324a73566ab786a3b3909f5d8990c89ad593bc5d15cc22cbf7a9ee8Result: e89e7abf2cc25cd1c53b59ad890c99d8f509393b6a78ab6635a724a3e7be6ab6

So as you can see, WE CAN have random number per transaction and because of it we can have now many ideas working without many steps.

We can actually create a Runtime random number per Transaction, so how can we improve that? The answer is right below. Again, Daniel Pan created this example with me, so enjoy!!

The Smart Contract — Improved Version

Compile the code above to get the AVM

55c56b51616556016c766b00527ac46c766b00c35a617c6583006c766b51
527ac46c766b51c353617c6571006c766b52527ac46c766b52c353617c65
5f006c766b53527ac46c766b53c355617c654d006c766b54527ac455c576
006c766b00c3c476516c766b51c3c476526c766b52c3c476536c766b53c3
c476546c766b54c3c46168124e656f2e52756e74696d652e4e6f74696679
616c756653c56b6c766b00527ac46c766b51527ac461682953797374656d
2e457865637574696f6e456e67696e652e476574536372697074436f6e74
61696e65726c766b52527ac46168184e656f2e426c6f636b636861696e2e
4765744865696768746168184e656f2e426c6f636b636861696e2e476574
4865616465726168124e656f2e4865616465722e476574486173686c766b
52c36168174e656f2e5472616e73616374696f6e2e476574486173687e6c
766b00c37eaa006c766b51c37f616c756651c56b6c766b00527ac4006c76
6b00c3617c652dff616c7566

The invocation result

Since the first part was proved that it’s possible to have different number per transaction, i’m gonna use only one result here:

As you can see, all numbers generated by this code is different from each other.

Additional Information

With this number random number you can also do the same as Jason L Perry did in the article i linked at the beginning.

If you want a random number inside within a range you can do as follows:

Really thanks to Daniel Pan that helped me a lot discussing and giving ideas about that issue.

Another thanks to Jason L Perry that provided me a interesting reading and was the first step for this new idea of using random number on NEO SmartContract.

For the ones that want to have more tips on how to deal with NEO SmartContract, there are an repository of mine on GitHub named HowToNEO that is just on the beginning, but i’ll put lot’s of stuffs there as soon as possible!!

Revision

01 — Use Block Hash with the Transaction Hash.

Get Best Software Deals Directly In Your Inbox

--

--