Gives a random 32-bit Int. This can be useful if you want to do low-level things with a cryptographically sound random 32-bit integer.
The value comes from running this code in Node using https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues:
import * as crypto from "node:crypto";
crypto.getRandomValues(new Uint32Array(1))[0]
Takes an
elm/randomRandom.Generatorand runs it using a randomly generated initial seed.type alias Data = { randomData : ( Int, Float ) } data : BackendTask FatalError Data data = BackendTask.map Data (BackendTask.Random.generate generator) generator : Random.Generator ( Int, Float ) generator = Random.map2 Tuple.pair (Random.int 0 100) (Random.float 0 100)The random initial seed is generated using https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues to generate a single 32-bit Integer. That 32-bit Integer is then used with
Random.initialSeedto create an Elm Random.Seed value. Then thatSeedused to run theGenerator.Note that this is different than
elm/random'sRandom.generate. This difference shouldn't be problematic, and in fact theBackendTaskrandom seed generation is more cryptographically independent because you can't determine the random seed based solely on the time at which it is run. Each time you callBackendTask.generateit uses a newly generated random seed to run theRandom.Generatorthat is passed in. In contrast,elm/random'sRandom.generategenerates an initial seed usingTime.now, and then continues with that same seed using usingRandom.stepto get new random values after that. You can see the implementation here. However,elm/randomis still not suitable in general for cryptographic uses of random because it uses 32-bits for when it steps through new seeds while running a singleRandom.Generator.