smartcontractkit / chainlink

node of the decentralized oracle network, bridging on and off-chain computation
https://chain.link
MIT License
6.83k stars 1.65k forks source link

[FEAT] VRF for Normal (Gaussian) Distribution #5393

Open Oighty opened 2 years ago

Oighty commented 2 years ago

Description Requesting a new VRF that returns a random number from a Normal Distribution. The simplest version would be a function that returns a random value from the standard normal distribution (Z, where mean = 0, st. dev = 1). The value could be stored as an int256 but evaluated as a fixed point number with 18 decimal places (similar to most ERC20 tokens). The sample from Z can be converted to any other normal distribution by the simple formula: sigma * Z + mu.

Motivation The current Chainlink VRF returns a random number from a uniform distribution. This is sufficient for generating outcomes at a specific probability or providing a random selection from group of choices. However, it is not a convenient or optimal solution for randomly selecting values for properties that vary from a mean. Examples include random rolls for game character or item attributes, size distribution for objects (height/width/length, weight, brightness, etc.), breeding time variation, growth rates, etc. Other potential areas not yet explored are in new DeFi constructs and generative art.

Justification Approximating a normal random number from the currently available options (i.e. uniform random numbers from Chainlink VRF) is possible, but hard. For example, the Box-Muller Transform allows you to approximate 2 values from a normal distribution from using two values from a uniform distribution from 0 to 1, but it requires several operations not native to solidity, sin, sqrt, and log2. You can use get these by using an advanced math library, but that adds to contract byte size and will cost a non-trivial amount of gas.

On the other hand, there are several optimized libraries in off-chain programming languages for quickly generating values from a normal distribution, e.g. NumPy

Additional Information Inspired by work on a NFT project that would generate random offspring from existing characters using hereditary attributes. Pointed to create an issue from twitter thread with Patrick Collins.

PatrickAlphaC commented 2 years ago

Thanks again for making this issue!