Skip to content

Commit 1e85380

Browse files
committed
feat: add random extraction helper-methods to UsingWitRandomness
1 parent c58eba9 commit 1e85380

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

contracts/UsingWitRandomness.sol

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract contract UsingWitRandomness {
1919
WIT_RANDOMNESS = WitRandomness(randomizer);
2020
}
2121

22-
/// Retrieves the result of keccak256-hashing the specified block number with the randomness value
22+
/// @notice Retrieves the result of keccak256-hashing the specified block number with the randomness value
2323
/// generated by the Witnet blockchain in response to the first non-failing randomize request solved
2424
/// after such block number.
2525
///
@@ -36,14 +36,62 @@ abstract contract UsingWitRandomness {
3636
);
3737
}
3838

39-
/// Generates a pseudo-random number uniformly distributed within the range [0 .. _faces),
39+
/// @notice Generates a pseudo-random number uniformly distributed within the range [0 .. _faces),
4040
/// by using the specified `_nonce` and `_seed`.
4141
/// @dev Fails under same conditions as `_fetchRandomnessAfter(uint256)` does.
42-
function _rollTheDice(uint64 _faces, uint256 _nonce, bytes32 _seed) internal pure returns (uint64) {
42+
function _randomDice(uint64 _faces, uint256 _nonce, bytes32 _seed) internal pure returns (uint64) {
4343
return Witnet.randomUniformUint64(
4444
_faces,
4545
_nonce,
4646
_seed
4747
);
4848
}
49+
50+
/// @notice Generates a pseudo-random uniformly distributed extraction, with repetitions.
51+
function _randomExtractionWithReps(
52+
uint64 _range,
53+
uint16 _extractions,
54+
bytes32 _seed
55+
)
56+
internal pure
57+
returns (uint64[] memory _balls)
58+
{
59+
unchecked {
60+
_balls = new uint64[](_extractions);
61+
for (uint16 _ix; _ix < _extractions; _ix ++) {
62+
_balls[_ix] = Witnet.randomUniformUint64(
63+
_range,
64+
_ix,
65+
_seed
66+
);
67+
}
68+
}
69+
}
70+
71+
/// @notice Generates a pseudo-random uniformly distributed extraction, with no repetitions.
72+
function _randomExtractionNoReps(
73+
uint64 _range,
74+
uint16 _extractions,
75+
bytes32 _seed
76+
)
77+
internal pure
78+
returns (uint64[] memory _balls)
79+
{
80+
unchecked {
81+
uint64[] memory _numbers = new uint64[](_range);
82+
for (uint64 _ix; _ix < _range; _ix ++) {
83+
_numbers[_ix] = _ix;
84+
}
85+
_balls = new uint64[](_extractions);
86+
for (uint16 _jx; _jx < _extractions; _jx ++) {
87+
uint64 _pos = Witnet.randomUniformUint64(
88+
_range - _jx,
89+
_jx,
90+
_seed
91+
);
92+
_balls[_jx] = _numbers[_pos];
93+
_numbers[_pos] = _numbers[_range - 1 - _jx];
94+
}
95+
}
96+
}
4997
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@witnet/solidity",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Wit/Oracle Solidity Framework for EVM-compatible chains",
55
"author": "Witnet Foundation <[email protected]>",
66
"license": "MIT",
@@ -16,7 +16,7 @@
1616
],
1717
"exports": {
1818
".": "./src/index.js",
19-
"./contracts/": "./contracts/",
19+
"./contracts/*": "./contracts/",
2020
"./utils": "./src/utils.js"
2121
},
2222
"files": [

0 commit comments

Comments
 (0)