solidityで任意の長さのuint8の疑似乱数の配列を受け取る関数
_getRandomUintArray
https://ethfiddle.com/0QtdzDXcl5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
pragma solidity ^0.4.24; contract SimpleStore { function _bytes32ArrayToUintArray(bytes32[] _bytes32Array) private pure returns(uint[]) { uint lenIn = _bytes32Array.length; uint lenOut = lenIn * 32; uint[] memory ret = new uint[](lenOut); uint cnt = 0; for(uint i = 0; i < lenIn; i++){ for(uint j = 0; j < 32; j++){ ret[cnt++] = uint(_bytes32Array[i][j]); } } return ret; } function _getRandomUintArray(uint len) private view returns(uint[]) { uint nowLocal = now; address msgSender = msg.sender; uint times = 1 + uint(len / 32); bytes32[] memory bytes32Array = new bytes32[](times); uint i; for(i = 0; i < times; i++){ uint seed = i + 1234567890; bytes32Array[i] = keccak256(seed, nowLocal, msgSender); } uint[] memory buf = _bytes32ArrayToUintArray(bytes32Array); uint[] memory ret = new uint[](len); for(i = 0; i < len; i++){ ret[i] = buf[i]; } return ret; } function test(uint len) public view returns(uint[]) { return _getRandomUintArray(len); } } |