I have an array like this in the array,

uint256[] public numberArr= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Each number in an array have their own weight,

uint256[] public weightNumberArr = [1, 3, 9, 7, 15, 18, 36, 22, 12, 6];

Here you can say that the weight of number 0 is 1 and for number 2 it is 3 like that. So here Chance of arriving number 6 is more than a chance of arriving number 0.

How can I generate a random number array of 0 to 9 by using weighted probability in solidity?

Any help will be highly appreciated!

  • Your question seems unrelated to Ethereum, but here is the same question from stackoverflow stackoverflow.com/questions/1761626/weighted-random-numbers.
    – Ismael
    Commented Sep 8, 2019 at 22:09
  • Thanks for reply @Ismael, I want to know how it can be unrelated to Ethereum, I was asking for it in solidity. Are you saying that this is not possible in solidity? Commented Sep 9, 2019 at 5:12
  • 1
    I mean the more general questions can be asked in stackoverflow, and there are many more developers there that can help you. Now with the algorithm on hand it should be easy to write in solidity ;)
    – Ismael
    Commented Sep 9, 2019 at 13:58
  • Thanks @Ismael for you kind reply, really appreciate that. Btw I figured that out and followed the same way you said. Thanks brother! Commented Sep 10, 2019 at 7:13

2 Answers 2


As hinted at by Ismael in the comments your question is mostly about the algorithm and is largely answered in the linked stackoverflow question.

However the part which is special for Solidity (and Ethereum) is the randomness part. Randomness in deterministic smart contracts is a tough question - there are good and bad solutions for it.

So once you decide on how to get your random number the rest is accomplished by simply implementing the given algorithm in Solidity. The only problematic part will be gas limits if there are too many numbers - iterating over the items may result in out of gas exceptions. In that case you have to find a different algorithm.

  • 1
    Thanks for detailed explanation, I really appreciate this. Just came up with the algorithm which consumes no gas and will work seamlessly even if there are too many numbers. Commented Sep 10, 2019 at 8:43
  • 2
    @Mr.Sirja Could you share that idea and algorithm?
    – Kristian
    Commented Apr 29, 2020 at 15:01
  • @Mr.Sirja Could you share that idea and algorithm?
    – 0xD1x0n
    Commented Jan 21, 2022 at 19:24

Here is an algorithm to achieve this:

  1. Add all the weight into a variable called totalWeight.
  2. Construct a binary tree with individual weights as the data nodes. The nodes of the tree is the sum of the weights of the child nodes.
  3. Now find a random number between 1 and totalWeight.
  4. Now traverse the tree to find which data has the number to select a weight randomly.

For example:

totalWeight = 500 randomNumber = 367

weights = [10, 100, 200, 190]

    110         390
 10     100 200     190   

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.