0

I have been able to use Random Number Generator from Chainlink.

But I need something that has biased Random Number.

What I mean is suppose a userA has 100 ERC20 Tokens balance but another userB has 200 ERC20 Tokens. The probability of selecting userB should be greater as he has more ERC20 tokens.

Also, there can be many number of users. I was able to split the random numbers and create a array of random numbers. But I cant figure out how to do it with bias.

This is what I am using currently.

for(uint i = 0; i < sizeOfLottery_; i++){
            // Encodes the random number with its position in loop
            bytes32 hashOfRandom = keccak256(abi.encodePacked(_randomNumber, i));
            // Casts random number hash into uint256
            uint256 numberRepresentation = uint256(hashOfRandom);
            // Sets the winning number position to a uint16 of random hash number
            winningNumbers[i] = uint16(numberRepresentation.mod(maxValidRange_));
        }

2 Answers 2

1

Step 1: store the ERC20 balances in an array if you haven't, like balances = [100, 200, 300] corresponding to 100 tokens of the 1st person, 200 for the second... Now calculate the totalWeight of the array, in my case 600 (100 + 200 + 300)

Step 2: create a random number which is in range of the totalWeight of the array, eg. make the random number between 0-600. you can just % the random number with totalWeight: randomnumber % totalWeight

Step 3: Selection logic

        for (uint256 i = 0; i < balances.length; i++) {
            totalWeight -= balances[i];
            if (randomNumber >= totalWeight) {
                index = i;
                break;
            }
        }

iteration explained: lets say your randomNumber is 350.

// first loop: totalWeight becomes 500(600 - 100). 350 is not greater than 500. rejected.

// second loop: totalWeight becomes 300(500 - 200). 350 is greater than 300. selected!

the point here is, chances of the randomNumber falling in the range of 300 is more than 200, and 200 is more than 100.

0

I would sum up the token amounts of participants and then multiply the random number so that it's between 0 and the total amount of tokens. So if, for example, the random number is between 0 and 100, you multiply it with 3, so it will be between 0 and 300. Then if the number is 0-100, the first player wins. If it's between 100-300, the second player wins.

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.