0

I need to generate a unique 128 bit number and know this number in my web3.js script.

Previously I tried this (create these numbers in my contract and retrieve them to web3.js). It seems impossible.

Now I consider an alternative solution: I instead generate a random number in web.js code and pass it to the contract to be stored in a map.

So the question: How in web3.js to generate a 128 bit random number with almost 100% probability than no other process using my contract generated the same number?

porton
  • 1,774
  • 5
  • 17
  • 38

2 Answers2

5

There is a util that can create "cryptographically strong pseudo-random HEX strings from a given byte size." You could then convert this to a number.

web3.utils.randomHex(size) --> documentation

Per the github web3js documentation the library frozeman/random is used to generate randomness

Steven V
  • 1,461
  • 10
  • 15
1

Try this:

const crypto = require("crypto");
const rand128 = "0x" + crypto.randomBytes(16).toString("hex");
goodvibration
  • 26,003
  • 5
  • 46
  • 86