# Triumph RNG

To minimize elements of chance in games with randomness, we want to synchronize randomness between users playing in a given match.&#x20;

As an example, in our flagship brick breaker game, the configuration of bricks in each level is random. When two users play, we want them to have the same exact grid.&#x20;

<div align="center"><figure><img src="/files/ASOZGKmjFdstCAiMiF92" alt=""><figcaption><p>Random number synchronization ensures fair gameplay in games with limited amounts of randomness.</p></figcaption></figure></div>

First, identify any sources of randomness in your game. This would be in places where you use normal random number libraries. You must replace these sources of randomness with the Triumph function `getNextRandom` , which returns a synchronized random number in (0,1).&#x20;

Below is an example from our flagship game.&#x20;

{% tabs %}
{% tab title="Unity" %}

```csharp
using TriumphSDK;

public class GameController : MonoBehaviour {
    public void generateBlocks() 
    {
        // There are 8 columns to generate blocks in
        for col in 1..<8 {
            // Triumph.getNextRandom() will give a seeded uniform 
            // random variable in (0,1). Since we want a random bool, we just 
            // check if this random is >= 0.5, which will happen 50 percent
            // of the time in expectation. 
            let triumphRandomSyncBool = Triumph.GetRandom() >= 0.5
            
            // Old implementation
            // if Bool.random() {
             
            // New implementation
            if triumphRandomSyncBool { 
                addBlock(inColumn: col)
            }
        }
    }
}
```

{% endtab %}

{% tab title="Swift" %}

```swift
import TriumphSDK

class YourGameViewController {
    // There are 8 columns to generate blocks in
    func generateBlocks() {
        for col in 1..<8 {
        
            // Triumph.getNextRandom() will give a seeded uniform 
            // random variable in (0,1). Since we want a random bool, we just 
            // check if this random is >= 0.5, which will happen 50 percent
            // of the time in expectation. 
            let triumphRandomSyncBool = Triumph.nextRandom() >= 0.5
            
            // Old implementation
            // if Bool.random() {
             
            // New implementation
            if triumphRandomSyncBool { 
                addBlock(inColumn: col)
            }
        }
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.triumpharcade.com/triumph-sdk/triumph-rng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
