'How does the RarePackFour smartcontract generate a new "unique" number from a given random number?

I'm trying to understand the RarePackFour smart contract from the game gods unchained. I noticed that they use a random number to generate other "random" (in parenthesis because i dont think the newly generated numbers are random). This the code im trying to understand. Could you help me understand what is happening here ?

function extract(uint random, uint length, uint start) internal pure returns (uint) {
        return (((1 << (length * 8)) - 1) & (random >> ((start * 8) - 1)));
    }

Bitwise operators are not realy a strong point for me so it would really help if you can help understand what is happening in the code.



Solution 1:[1]

Let's go with example:

length = 1
start = 1
random = 3250 # Binary: 0b110010110010

1. ((1 << (length * 8)) - 1) = 2^8 - 1 = 255 = 0B11111111 # Binary

2. (random >> ((start * 8) - 1))) = 0b110010110010 >> 7 = 0B11001 # Decimal 25


0B11111111 & 0B11001 = 0B11001 = 25

Usually if length * 8 > ((start * 8) - 1), the function returns random / (start * 8 - 1). Notice that it's only integer calculation in Solidity.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PySoL