// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Shuffle {

    constructor() {
        ranking = [0,1,2,3,4,5,6,7];
        currentSeason = SeasonType.Spring;

    enum SeasonType {

    uint256[] public ranking;
    SeasonType public currentSeason;

    function shuffleRankingsManual() external {
        uint256[] memory arr = ranking;

        for (uint256 i = 0; i < arr.length; i++) {
            uint256 randomNumber = uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty, block.gaslimit, i)));
            uint256 j = i + (randomNumber % (arr.length - i));

            uint256 temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;

        ranking = arr;

        uint256 randomNumber1 = uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty, block.gaslimit)));

        uint256 newSeasonIndex = randomNumber1 % 4;

        currentSeason = SeasonType(newSeasonIndex);

The shuffleRankingsManual() function shuffles the array ranking randomly using the Fisher-Yates shuffle algorithm.

The issue I'm running into is when deployed on the Sepolia testnet, it gives me failed and successful transactions without any change of code or gas limit or any other settings, I simply call the function again and again and it produces mixed results, here is a screen shot.


I been trying to debug it but have led me no where. If someone can have a look that would be greatly appreciated.

1 Answer 1


I think you might be hitting out of bounds for your array, try uint256 j = i + (randomNumber % (arr.length - i - 1)); instead? This would only show up sometimes because the random number would not always fall off the end of the array.

  • Thanks man, but I think I figured it out. It seems that metamask can't accurately estimate gas with it comes to this function. I have to manually adjust it so that there's enough gas for the txs. I appreciate your help, all the best!
    – East
    Commented Apr 5, 2023 at 4:45

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.