I am trying to assign unique tickets for users. The user can choose the number of tickets and get an allotment. The allocated tickets will be mapped to users.
pragma solidity ^0.8.11;
contract UniqTicket
{
// Initializing the state variable
uint public randNonce = 0;
mapping(address => uint256[]) public participants;
uint256[] public allTickets;
function randMod(uint _modulus) public returns(uint){
randNonce++;
return uint(keccak256(abi.encodePacked(block.timestamp, msg.sender, randNonce, block.difficulty))) % _modulus;
}
function allotTickets(uint256 size) public{
for(uint i=0; i<size; i++) {
uint256 ticket = randMod(1000);
participants[msg.sender].push(ticket);
allTickets.push(ticket);
}
}
function getTickets(address _user) public view returns(uint256[] memory) {
return participants[_user];
}
function fetchAllTickets() public view returns(uint256[] memory) {
return allTickets;
}
}
When users choose allotTickets(200).
The user is getting duplicate numbers. Though randNonce is a unique counter.
Looking out for a solution, how to get unique numbers.
Sample Output:
[427,257,673,731,304,357,2,959,236,931,766,322,929,42,69,897,902,116,972,408,27,397,13,141,337,238,520,680,847,657,44,630,649,722,715,81,126,569,952,874,925,354,907,101,649,548,259,8,678,10,297,256,371,369,338,355,446,248,36,639,169,742,802,391,886,117,230,781,230,279,693,9,532,456,276,148,105,863,134,161,19,954,231,949,849,477,211,305,570,281,575,548,340,348,758,15,794,311,634,639,753,50,33,856,59,142,505,201,331,513,913,801,660,739,830,641,888,975,481,236,473,624,243,86,976,871,606,736,726,780,571,53,98,470,702,437,33,688,332,639,392,720,751,107,377,544,235,773,625,544,451,315,68,559,217,491,401,75,549,781,267,217,308,430,776,163,300,438,631,125,689,787,503,805,277,828,430,377,133,122,343,913,637,712,862,247,348,964,824,189,711,666,730,43,704,896,829,771,696,983]
Thank you