Random number generation

FQL does not provide random number generation, as its operations attempt to be as deterministic as possible.

This is a strange feature to be missing from a language and I don’t see how the attempt to be deterministic is helpful.

I need to construct a UDF and connect it with a GraphQL mutation. The UDF requires multiple steps in a transaction where I need to randomly select an item in an array. I have a similar use case where I need to generate a random dice roll. This is trivial in any other language but downright impossible in FQL.

One case I can solve in my host language as the docs recommend (though it is still cumbersome to do so) but the other case is less reliable with a dynamic set of items to choose from and multiple concurrent actors adding/deleting those items.

Would you mind linking me to what you are quoting?

1 Like

Small note on what the docs are probably implying, determinism is very important in Calvin (an algorithm that FaunaDB is inspired by) Consistent Backends and UX: How Do New Algorithms Help? | CSS-Tricks - CSS-Tricks. However, I believe the docs aren’t entirely accurate in this case since the FaunaDB modification works quite differently and I do think that we could offer a random function. We just don’t offer it at this point but to my knowledge it’s perfectly possible (would be hard with pure Calvin if I’m not mistaking, but is perfectly possible in the FaunaDB algorithm)

@ben is my assumption correct? In that case, I’ll consider it a feature request and I’ll add a ticket to Jira.

Yeah, there is no technical barrier to adding random number generation, because otherwise something like NewID wouldn’t work either :slight_smile:

Hey, I just created something in FQL that might be sufficient. I wanted to see if I could use NewId and some math to make random strings. This could easily be modified to pick random items from an array. Check it out here https://github.com/shiftx/faunadb-fql-lib#randomstring

2 Likes

Thanks @eigilsagafos! That should work just fine. Also I love the lib you created.

#faunadbFqlLibForTheWin!

Daniel_Steigerwald shared an interesting psuedo-random number generation. He’s also hyper-focused on a pure functional style, and the pseudo random generation methods help with that (for context).

2 Likes