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?

2 Answers 2

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

1

Try this:

const crypto = require("crypto");
const rand128 = "0x" + crypto.randomBytes(16).toString("hex");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.