I am trying to implement a smart contract, that takes in a discreet distribution and returns a loaded random number. The higher the number higher the chance from 1-10000.
**anyone has any experience with this or ideas?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityHere is an example from "Building Games with Ethereum Smart Contracts".
contract SatoshiDice {
struct Bet {
address user;
uint block;
uint cap;
uint amount;
}
uint public constant FEE_NUMERATOR = 1;
uint public constant FEE_DENOMINATOR = 100;
uint public constant MAXIMUM_CAP = 100000;
uint public constant MAXIMUM_BET_SIZE = 1e18;
address owner;
uint public counter = 0;
mapping(uint => Bet) public bets;
event BetPlaced(uint id, address user, uint cap, uint amount);
event Roll(uint id, uint rolled);
function SatoshiDice () public {
owner = msg.sender;
}
function wager (uint cap) public payable {
require(cap <= MAXIMUM_CAP);
require(msg.value <= MAXIMUM_BET_SIZE);
counter++;
bets[counter] = Bet(msg.sender, block.number + 3, cap,
msg.value);
BetPlaced(counter, msg.sender, cap, msg.value);
}
function roll(uint id) public {
Bet storage bet = bets[id];
require(msg.sender == bet.user);
require(block.number >= bet.block);
require(block.number <= bet.block + 255);
bytes32 random = keccak256(block.blockhash(bet.block), id);
uint rolled = uint(random) % MAXIMUM_CAP;
if (rolled < bet.cap) {
uint payout = bet.amount * MAXIMUM_CAP / bet.cap;
uint fee = payout * FEE_NUMERATOR / FEE_DENOMINATOR;
payout -= fee;
msg.sender.transfer(payout);
}
Roll(id, rolled);
delete bets[id];
}
function fund () payable public {}
function kill () public {
require(msg.sender == owner);
selfdestruct(owner);
}
}
As the explanation for this kind of contracts is beyond a StackExchange answer, I'd recommend reading the book and/or more about random number generation on-chain.