I’m trying to develop a weighted probability selection algorithm on BSC and I think I need an algorithm that can generate a random number within a given range for that.
What would be a “good enough” way to achieve that? I know there’s a lot of problems with RNG in smart contracts, but is there a solution that’s practically safe enough for BSC? For example if I’m not mistaken the miners all belong to Binance there, so as long you trust Binance (I know that this goes against the philosophy of crypto…) there shouldn’t be mining attacks.
I’m in the same boat as you. My team has spent sometime looking for alternatives on BSC for RNG. Chainlink’s VRF is coming soon to BSC, but not here yet.
The approach we are taking is to do some basic RNG for our ERC721s, then at a later date, when Chainlink’s VRF comes out, we will migrate to it.
The problem is as you say, the miners will just manipulate the RNG to benefit them so there isn’t a great solution right now until Chainlink VRF comes out.
Here’s some things to consider when building your own implementation.
You are right, random number generation in smart contracts is always a tricky topic due to the deterministic nature of the blockchain. On BSC, the situation is a bit easier, since most validators are controlled by Binance, and the probability of manipulation is lower than in fully decentralized networks.
For a "good enough" level of security, a combination of several sources is often used, for example:
block.timestamp - although it can be manipulated a little, but within a limited range;
block.difficulty or block.prevrandao (if supported);
hash of the previous block blockhash(block.number - 1).
By combining these values and applying a hash function, you can get a pseudo-random number that in most cases will be quite unpredictable.
If you need increased security, you can consider integrating with external oracles, such as Chainlink VRF, which provides a cryptographically secure source of random numbers.
But if you trust Binance and their validators, then the combination of on-chain parameters is a working solution for most practical tasks on BSC. The main thing is not to use just block.timestamp alone, but to mix it with other values.