pragma solidity ^0.8.0;
contract RandomWinnerGenerator {
// The array to store all the participants
address[] public participants;
// Event to notify the winner
event Winner(address winner);
// Function to add participants
function participate() public payable {
participants.push(msg.sender);
}
// Function to select a random winner
function selectWinner() public {
// Check if there are enough participants
require(participants.length > 0, "No participants found.");
// Generate a random index based on the number of participants
uint randomIndex = uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty))) % participants.length;
// Declare the winner
address winner = participants[randomIndex];
// Trigger the Winner event
emit Winner(winner);
}
}
pragma solidity ^0.8.0;
contract RandomWinnerGenerator {
// The array to store all the participants
address[] public participants;
// Event to notify the winner
event Winner(address winner);
// Function to add participants
function participate() public payable {
participants.push(msg.sender);
}
// Function to select a random winner and distribute the funds
function selectWinner() public {
// Check if there are enough participants
require(participants.length > 0, "No participants found.");
// Generate a random index based on the number of participants
uint randomIndex = uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty))) % participants.length;
// Declare the winner
address winner = participants[randomIndex];
// Transfer the funds to the winner's wallet
winner.transfer(address(this).balance);
// Trigger the Winner event
emit Winner(winner);
}
}
pragma solidity ^0.8.0;
contract RandomWinnerGenerator {
// The array to store all the participants
address[] public participants;
// The percentage of the jackpot for each winner
uint public firstWinnerPercentage = 70;
uint public secondWinnerPercentage = 20;
uint public thirdWinnerPercentage = 10;
// Event to notify the winners
event Winner(address winner, uint amount);
// Function to add participants
function participate() public payable {
participants.push(msg.sender);
}
// Function to select the winners and distribute the funds
function selectWinners() public {
// Check if there are enough participants
require(participants.length >= 3, "At least 3 participants are required.");
// Generate random indices based on the number of participants
uint firstWinnerIndex = uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty))) % participants.length;
uint secondWinnerIndex = (firstWinnerIndex + 1) % participants.length;
uint thirdWinnerIndex = (firstWinnerIndex + 2) % participants.length;
// Declare the winners
address firstWinner = participants[firstWinnerIndex];
address secondWinner = participants[secondWinnerIndex];
address thirdWinner = participants[thirdWinnerIndex];
// Calculate the amount for each winner
uint totalJackpot = address(this).balance;
uint firstWinnerAmount = (firstWinnerPercentage * totalJackpot) / 100;
uint secondWinnerAmount = (secondWinnerPercentage * totalJackpot) / 100;
uint thirdWinnerAmount = (thirdWinnerPercentage * totalJackpot) / 100;
// Transfer the funds to the winners' wallets
firstWinner.transfer(firstWinnerAmount);
secondWinner.transfer(secondWinnerAmount);
thirdWinner.transfer(thirdWinnerAmount);
// Trigger the Winner events
emit Winner(firstWinner, firstWinnerAmount);
emit Winner(secondWinner, secondWinnerAmount);
emit Winner(thirdWinner, thirdWinnerAmount);
}
}