# Winner Selection

## Overview

Our raffle system uses a transparent, verifiable, and fair process to select winners. The selection happens entirely on the Solana blockchain, which means it's tamper-proof and publicly verifiable.

## The Two-Step Process

We use a two-step process to ensure maximum fairness:

1. **Random Ticket Selection**: First, our smart contract generates a random winning ticket number
2. **Winner Identification**: Then, the system identifies which user purchased that ticket

## Technical Implementation

### **Step 1: Random Ticket Drawing**

The smart contract uses Solana's SlotHashes as a source of randomness. Here's what happens:

1. The contract accesses the most recent SlotHashes sysvar (system variable) on Solana
2. It extracts the most recent block hash (the first 8 bytes of data)
3. The contract combines this hash with the current timestamp to create a unique seed value
4. Using this seed, it generates a number between 0 and the total number of tickets sold (using modulo operation)
5. This number becomes the winning ticket

This implementation ensures:

* No one (not even us) can predict which ticket will win
* The randomness comes from the blockchain itself
* Anyone can verify the process by checking the transaction on-chain

### **Step 2: Winner Identification**

Once the winning ticket number is determined:

1. The smart contract searches through all entry records to find which entry contains the winning ticket
2. Each entry record contains:
   * The owner's wallet address
   * The ticket count
   * The starting ticket index
3. The entry is found where: `starting_index ≤ winning_ticket < (starting_index + ticket_count)`
4. The owner of this entry is identified as the winner

## Verification

Users can verify this process by:

1. Checking the raffle state on-chain, which includes the winning ticket number
2. Reviewing the transaction where the winner was determined
3. Confirming that their entry's ticket range does or doesn't include the winning number

## Example

Let's say a raffle sold 100 tickets total:

* Alice bought tickets 0-9 (10 tickets)
* Bob bought tickets 10-29 (20 tickets)
* Charlie bought tickets 30-99 (70 tickets)

If the random process selects ticket #23:

1. The system identifies that ticket #23 falls within Bob's range (10-29)
2. Bob is declared the winner
3. This result is recorded on-chain and cannot be altered

The probability of winning is directly proportional to the number of tickets purchased - Bob had a 20% chance with his 20 tickets.
