Skip to content
Discussions/App Development/Generate random integersForum ↗

Generate random integers

App Development5 posts937 views11 likesLast activity May 2022
AR
Arne_GebertOP
Dec 2020

I’m looking for a way to generate (pseudo-)random Integers in DAML. I saw the coin flip example in the tutorials, but I’m wondering: is there a simple random function in DAML I can also use (akin to the random-function in Haskell)?

I looked through the language docs, but couldn’t find anything fitting.

BE
bernhard
Dec 2020

Welcome to the forum @Arne_Gebert! There is no pseudo-random number generator in the StdLib, and in part that’s to discourage people attempting to do random number generation in DAML. It’s not possible. The submitting party has access to all information that goes into the deterministic calculation of the transaction so they can predict exactly what any “random” number will come out as. The sha256 function is about as random as it gets in daml.

LU
Luciano
Dec 2020

Hi Arne. Bernhard is right; there is no way to create a secure random number in general. If all you need is a pseudo-random generator though, for non-secure purposes, it’s pretty easy to code this up with a State monad. RNG is the canonical textbook example for this monad.

For example here:

learnyouahaskell.com

For a Few Monads More - Learn You a Haskell for Great Good!

SA
Sam_Savage
May 2022

I’m not sure the existing answers will remain true indefinitely according to the following blog:

blog.digitalasset.com

Calling Any API Through Daml

Editor’s note: This post is the fifth in our series “How to Make the Most of the Daml Application Framework.” Part 1 introduced the fundamentals of Daml/DABL architecture, and Part 2 explained how tha

we’ll consider how DABL talks to other APIs to speed integration of third-party functionality into Daml applications. …
Digital Asset is building a layer that lets Daml developers plug APIs into Daml workflows

Could this allow for (computational) non-determinism? Since one could introduce functions that are not referentially transparent?

We could even go one step further and consider physical non-determinism by calling out to ANU QRNG quantum random number generator API: API documentation – ANU QRNG … of course that depends on your philosophical interpretation of quantum mechanics :joy:

Also

and in part that’s to discourage people attempting to do random number generation in DAML

Perhaps this is assuming that there exists no practical application of randomness in smart contracts? I can think of some counterexamples:

  • Validator selection in POS is itself a meta-application of randomness
  • The gaming industry (formerly “gambling” industry) currently suffers from exit scams, cheating, and high-rakes (via monopolization). If smart contracts could serve as a backend for gaming, this could eliminate these perils.
LU
Luciano
May 2022
Sam_Savage:

Could this allow for (computational) non-determinism? Since one could introduce functions that are not referentially transparent?

The use of non-determinism in Daml programs is not possible at a fundamental level.

If you think about the architecture of a daml ledger, each party of a transaction in that network needs to be able to re-run Daml code to certify the resulting updates/contracts are valid. Analogously, you can think of how a blockchain processes transactions, and how each node needs to be able to re-compute hashes to verify the veracity of submitted transactions. That code needs to be deterministic, by design. It’s the same problem here.

We can always bring in entropy from outside, into the ledger, as a function (choice) parameter, as you’re suggesting. But the Daml transactions themselves are always deterministic.

← Back to Discussions