1

I have an array of integers like this,

uint256[] public numberArr = [0,0,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,5,5,5]

What can be the best way to shuffle this array in solidity

1

Try this:

function shuffle() external {
    for (uint256 i = 0; i < numberArr.length; i++) {
        uint256 n = i + uint256(keccak256(abi.encodePacked(now))) % (numberArr.length - i);
        uint256 temp = numberArr[n];
        numberArr[n] = numberArr[i];
        numberArr[i] = temp;
    }
}
  • 1
    Thanks for this @goodvibration – Mr. Sirja Sep 8 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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