Skip to content

Commit

Permalink
Update rand.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
juntao committed Apr 4, 2019
1 parent 623c820 commit 7c9409e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/rand.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,27 @@ Example 1
}
}
**Note** You should NOT call `rand()` in a `view` or `pure` function. If the random number does not need to be recorded on the blockchain (i.e., outside of a transaction in a `view` function executed on a single node), it does not need to be generated by the blockchain. The calling application should simply generate a random number locally -- it is much cheaper in terms of resource consumption.
.. WARNING::
You should NOT call `rand()` in a `view` or `pure` function. If the random number does not need to be recorded on the blockchain (i.e., outside of a transaction in a `view` function executed on a single node), it does not need to be generated by the blockchain. The calling application should simply generate a random number locally -- it is much cheaper in terms of resource consumption.

The example below shows how to generate a series of random numbers between 0 and 99. You can invoke `createRand()` multiple times within a block (i.e., wthin 10s) or across multiple blocks to see the results.

.. code-block:: Lity
pragma lity >=1.2.6;
contract RandDemo {
uint[] s;
function createRand () public {
s.push(rand() % 100);
}
function last5Rands () public view returns (uint, uint, uint, uint, uint) {
require (s.length >= 5);
return (s[s.length-1], s[s.length-2], s[s.length-3], s[s.length-4], s[s.length-5]);
}
}

0 comments on commit 7c9409e

Please sign in to comment.