Why is generating random numbers so slow?

Hi guys, I use Random from Motoko to generate random numbers
But I found that the generation time needs to be very long, is there any good way?

  var blob=await Random.blob();
  var seednum:Nat=Nat8.toNat(Random.byteFrom(blob));
1 Like

Hi @HelloRickey,

The delay is intentional, it guarantees that you actually get fresh randomness which was created after the request for randomness was made.

5 Likes

Yes, exactly.

But luckily, you can do some management of when you want to pay this cost using the Finite class within the Random module. That class will give you entropy derived from one of those (costly) calls and will manage when it has run out, requiring another call to the actual system’s (real) entropy source.

Here’s a link to the code in base package, with doc comments there that explain more.

5 Likes