//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; contract Random { uint256 _seed; // The upper bound of the number returns is 2^bits - 1 function bitSlice(uint256 n, uint256 bits, uint256 slot) public pure returns(uint256) { uint256 offset = slot * bits; // mask is made by shifting left an offset number of times uint256 mask = uint256((2**bits) - 1) << offset; // AND n with mask, and trim to max of 5 bits return uint256((n & mask) >> offset); } function maxRandom() public returns (uint256 randomNumber) { _seed = uint256(keccak256( _seed, block.blockhash(block.number - 1), block.coinbase, block.difficulty )); return _seed; } // return a pseudo random number between lower and upper bounds // given the number of previous blocks it should hash. function random(uint256 upper) public returns (uint256 randomNumber) { return maxRandom() % upper; } } contract FamilyName is Random{ struct Name { string name; } Name[] names; //Is name used filter mapping(uint => bool) public idToAvailable; mapping(bool => uint) public avaiableNameCount; function FamilyName() public { //Create some names _addName("Terlizzi0"); _addName("Corsaro1"); _addName("Maiolo2"); _addName("Verno3"); _addName("Buccheri4"); _addName("Abele6"); _addName("Codispoti7"); _addName("Fucile8"); _addName("Schiappa9"); _addName("Posillico10"); } function _addName(string _name) internal { //Create a name uint id = names.push(Name(_name)) -1; idToAvailable[id] = true; avaiableNameCount[true]++; } function _burnName(uint _nameId) public { require(idToAvailable[_nameId] == true); idToAvailable[_nameId] = false; avaiableNameCount[true]--; avaiableNameCount[false]++; } function _consumeName() public returns (bytes32) { uint[] memory availableNames = getAvailableNames(); uint randNr = random(availableNames.length); for (uint256 i = 0; i < availableNames.length; i++) { if(i == randNr){ string memory name = names[availableNames[i]].name; _burnName(availableNames[i]); } } return stringToBytes32(name); } function getAvailableNames() public view returns (uint[]){ uint[] memory result = new uint[](avaiableNameCount[true]); uint counter = 0; for (uint i = 0; i < names.length; i++){ if(idToAvailable[i] == true){ result[counter] = i; counter++; } } return result; } function stringToBytes32(string memory source) internal pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); if (tempEmptyStringTest.length == 0) { return 0x0; } assembly { result := mload(add(source, 32)) } } }
0.4.18