@@ -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}
0 commit comments