0

I need to generate a random number for betting games. Is this pattern safety solution?

mapping(uint => address) playerMapping;

address[] players;

// here is contract will save block.number of player
function bet() { 
  ...   
  playerMapping[block.number] = msg.sender;
  players.push(playerMapping[block.number]); // blockhash(block.number) will be unknown at this stage 
  ... 
}

// this function will called by bot
function reveal(uint blockNumber) {
   ...
   uint random = blockhash(blockNumber); 
   ... 
}
3
  • Not really. Your sequence of random numbers will be an increasing sequence. That by itself eliminates true randomness. To make it worse, those numbers will likely increase by 0 or 1 in most cases (and even if not, the increase will not be uniformly distributed, but will most likely concentrate around the small values), which will hinder randomness even further. Jan 23, 2019 at 13:00
  • @goodvibration I think you're confused? This code uses the block hash of a future block. Not tamper-proof, but only manipulable by the miner of that future block. Without such manipulation, it should be unpredictable. But be aware that if the hash is not revealed within 256 blocks, it's no longer available to smart contracts.
    – user19510
    Jan 23, 2019 at 16:28
  • @smarx: Yes, you're right, I was looking at push(playerMapping[block.number]), and to make it worse, ignored the playerMapping (so I figured that the list of random numbers was just a list of block numbers). Jan 23, 2019 at 17:12

0

Browse other questions tagged or ask your own question.