2

I found this weird gist online for generating random numbers. Please remember that, despite the code's comments, generating random numbers on-chain is insecure for values more than 2.5 ETH.

// this labyrinth contract as a public utility can be used as an "entropy crawler", 
// using the state itself as a source of entropy (generated entirely from human social coordination 
// that is highly random. )

contract Labyrinth {

  uint entropy;

  function getRandomNumber() public returns (uint) {
    entropy ^= uint(blockhash(entropy % block.number));
    return entropy;
  }

}

What is the "^=" operator and what does it do? I tried searching in javascript, solidity, and other languages through Google but nothing is coming up.

Source: https://gist.github.com/resilience-me/8e877bc39a9f8d86f17f08bc62d71d70

5

This is perhaps more of a general programming question than anything specific to Solidity...

It's a bitwise exclusive OR (XOR - ^), combined with an assignment operator (=).

a ^= b is equivalent to a = a ^ b, in a similar way to a = a + 1 being written a += 1.

  • Thanks. Yea I figured it was that, but then in the context of the rest of the function... ... blockhash is supposed to accept inputs from 0 to 256, right?... I guess I am just wondering how the above function works, if entropy is a really big number, divided by block.number ( another really big number) , there's no guarantee the modulo will be less than 256? or is there? i guess the above contract doesn't even work? (it looks like it should though by everything i've been reading of this guy) – nick carraway Feb 28 at 22:45
  • FYI, I asked that comment as a question here: ethereum.stackexchange.com/questions/67786/… – nick carraway Feb 28 at 22:56
  • Sorry for missing your follow-up comment. Agree with your and Rob's assertions in the other question :-) – Richard Horrocks Mar 1 at 9:26

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.