# Winner Selection

The winner selection process in FunStake is designed to ensure fairness and transparency, leveraging the security and randomness capabilities of the NEAR Protocol.&#x20;

Here’s a step-by-step breakdown of how we select the winner(s) for each game session:

1. **Random Number Generation:**
   * At the end of each game session, FunStake uses **NEAR’s randomSeed()** function to generate a cryptographically secure random seed. This ensures that the random number generation process is both secure and unpredictable.
   * The random seed is converted into a **Uint8Array**, an array of 8-bit unsigned integers, which is then used to build a large BigInt value. This process combines all the random bytes into a single, large random number.
2. **Calculating the Winning Number**:
   * The large BigInt value generated from the random seed is then subjected to a modulo operation with the **total number of tickets** in the game session. This operation ensures that the winning number falls within the range of available tickets. For instance, if the total number of tickets is **100**, the winning number will be **between 0 and 99**.
   * If there are no tickets (to prevent division by zero), the range defaults to **1**. This ensures that the winner selection process is always valid.
3. **Determining the Winner:**
   * Once the winning number is calculated, the protocol iterates through the tickets of all participants to find which user’s ticket range includes the winning number. **Each participant’s ticket range is determined by their accumulated tickets, starting from the cumulative total of previous participants.**
   * For example, if Participant A has **50** tickets **(range 0-49)** and Participant B has **30** tickets **(range 50-79)**, and the **winning number is 55**, Participant B would be the winner since **55 falls within their ticket range**.
4. **Multiple Winners (Future Feature):**
   * Currently, FunStake supports one winner per game session. However, the protocol is designed to be flexible and can be adjusted to support multiple winners in the future. \
     This will allow the final prize to be shared among multiple participants, enhancing the inclusivity of the game.

By utilizing NEAR’s robust random number generation and a transparent selection process, FunStake ensures that every participant has a fair chance of winning based on their ticket count and timing of their deposits.
