0

This question already has an answer here:

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); 
   ... 
}

marked as duplicate by Malone, Rosco Kalis, Achala Dissanayake, Ismael, GrandFleet Jan 23 at 16:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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. – goodvibration Jan 23 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. – smarx Jan 23 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). – goodvibration Jan 23 at 17:12

Browse other questions tagged or ask your own question.